Ver código fonte

Minor Menu Bar Updates

Andreas T. Meyer-Berg 3 anos atrás
pai
commit
44ff2b162f

+ 113 - 33
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/menuBar/MenuBarNetworkExamples.java

@@ -9,6 +9,8 @@ import java.util.Map.Entry;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 
+import org.apache.commons.math3.distribution.NormalDistribution;
+
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ExampleAnomalyController;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ImportController;
@@ -27,6 +29,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.RoomStatus;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolCollectorDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensorDevice;
@@ -38,10 +41,12 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureProdu
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureSensor;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.AbstractEvent;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.CountingMetric;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.Manipulation_RandomMove;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation.SWCKMeansClustering;
 
 public class MenuBarNetworkExamples extends JMenu{
 
@@ -827,6 +832,7 @@ public class MenuBarNetworkExamples extends JMenu{
 		pBroker.setResponseTime((short)2);
 		networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
 		networkController.addConnection(mqtt);
+		mqtt.setLabel((short) 0);
 		
 		/**
 		 * Room
@@ -841,20 +847,20 @@ public class MenuBarNetworkExamples extends JMenu{
 		/**
 		 * Room Thermostat
 		 */
-		SmartTemperatureSensor floatSensor = new SmartTemperatureSensor(roomName + " Thermostat", room);
-		floatSensor.setFSinfoName("home/" + roomName + "/sensorTemp");
-		networkController.addLinkToDevice(zigbee, floatSensor);
-		networkController.moveSmartDevice(floatSensor, 300, 300, 50);
-		floatSensor.setFSmin(19.0f);
-		floatSensor.setFSmax(21.0f);
-		floatSensor.setFSval(19.0f);
-		networkController.addSmartDevice(floatSensor);
-		Port pFloatSensor = new Port(floatSensor, (short)1883, 15000);
+		SmartTemperatureSensor heatSensor = new SmartTemperatureSensor(roomName + " Thermostat", room);
+		heatSensor.setFSinfoName("home/" + roomName + "/sensorTemp");
+		networkController.addLinkToDevice(zigbee, heatSensor);
+		networkController.moveSmartDevice(heatSensor, 300, 300, 50);
+		heatSensor.setFSmin(19.0f);
+		heatSensor.setFSmax(21.0f);
+		heatSensor.setFSval(19.0f);
+		networkController.addSmartDevice(heatSensor);
+		Port pFloatSensor = new Port(heatSensor, (short)1883, 15000);
 		pFloatSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
 		pFloatSensor.setStatus(Port.SENDING);
 		pFloatSensor.setLastTrigger(-357L);
 		networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
-		
+		heatSensor.setLabel((short) 0);
 		/*
 		 * Add Heater (every second update)
 		 */
@@ -871,7 +877,7 @@ public class MenuBarNetworkExamples extends JMenu{
 		pHeaterDevice.setTriggerHandler(new NormalDistributionHandler(15000, 500));
 		pHeaterDevice.setLastTrigger(-1231L);
 		networkController.addDeviceToConnectionAndProtocol(pHeaterDevice, mqtt,1);
-		
+		heaterDevice.setLabel((short) 0);
 		
 		/*
 		 * Add a light
@@ -886,6 +892,7 @@ public class MenuBarNetworkExamples extends JMenu{
 		pSmartLight.setStatus(Port.SENDING);
 		pSmartLight.setLastTrigger(-1207L);
 		networkController.addDeviceToConnectionAndProtocol(pSmartLight, mqtt,1);
+		smartLight.setLabel((short) 0);
 		
 		/*
 		 * Add light Sensor
@@ -900,7 +907,7 @@ public class MenuBarNetworkExamples extends JMenu{
 		pLightSensor.setStatus(Port.SENDING);
 		pLightSensor.setLastTrigger(-1337L);
 		networkController.addDeviceToConnectionAndProtocol(pLightSensor, mqtt,1);
-		
+		lightSensor.setLabel((short) 0);
 		
 		/* 
 		 * Update visualization 
@@ -916,7 +923,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			/**
 			 * Instances of the packet Sniffers
 			 */
-			PacketSniffer snifferEM = null, snifferKNN = null, snifferHC = null;
+			PacketSniffer snifferKNN = null; //, snifferEM = null, snifferHC = null;
 			/*
 			 * Import Example PacketSniffer algorithms
 			 */
@@ -927,8 +934,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			Class<? extends PacketSniffer> hc = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/HierarchicalClustering.java"));
 			snifferHC = hc.newInstance();*/
 			
-			Class<? extends PacketSniffer> knn = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/KMeansClustering.java"));
-			snifferKNN = knn.newInstance();
+			snifferKNN = new SWCKMeansClustering();
 			
 			System.out.println("Check 2: Imported");//TODO
 			/*
@@ -962,11 +968,55 @@ public class MenuBarNetworkExamples extends JMenu{
 			sim.resetSimulation();
 			sim.setStepDuration(hour);
 			sim.setPrintPackets(false);
-			sim.setEndTime(24*hour);
+			long endTime = 24*hour;
+			sim.setEndTime(endTime);
 
-			heaterDevice.setFSmin(19f);
-			heaterDevice.setFSmax(23f);
-			heaterDevice.setFSval(20f);
+			/**
+			 * Training events
+			 */
+			System.out.println("Create training events");
+			/**
+			 * Light Events (random switch every 30min+-15min
+			 */
+			NormalDistribution lightDist = new NormalDistribution(hour/2, hour/4);
+			long eventTime = 0;
+			while(eventTime < endTime + hour/2) {
+				/**
+				 * At least 1 second between events
+				 */
+				eventTime+=Math.max(1000, Math.round(lightDist.sample()));
+				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
+					
+					@Override
+					public void simulateEvent(long time) {
+						smartLight.setBSval(!smartLight.isTrueStatus());
+						smartLight.setTrueStatus(!smartLight.isTrueStatus());
+					}
+				});
+				
+			}
+			/**
+			 * Heater Events (random switch every 2h +- 30min
+			 */
+			NormalDistribution tempDist = new NormalDistribution(2*hour, hour/2);
+			eventTime = 0;
+			while(eventTime < endTime + 1 * hour) {
+				/**
+				 * At least 1 second between events
+				 */
+				eventTime+=Math.max(1000, Math.round(tempDist.sample()));
+				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
+					
+					@Override
+					public void simulateEvent(long time) {
+						float newTemp = (float)(heaterDevice.getFSmax() + Math.random() * (heaterDevice.getFSmax()-heaterDevice.getFSmin()));
+						heaterDevice.setFSval(newTemp);
+						heaterDevice.setFSval(newTemp);
+					}
+				});
+				
+			}
+			
 			System.out.println("Training:");//TODO
 			for(int i=0; i<24; i++)
 				sim.getSimulationManager().simulateTimeIntervall(i*hour, hour);
@@ -994,7 +1044,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			 */
 
 			System.out.println("Test w/0 anomalies:");//TODO
-			for(int i=24; i<25; i++)
+			for(int i=24; i<26; i++)
 				sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
 			
 			new_time = System.currentTimeMillis();
@@ -1003,23 +1053,53 @@ public class MenuBarNetworkExamples extends JMenu{
 			currentTime = new_time;
 			
 			/**
+			 * Light Anomaly 1h
 			 * Light != Sensor
-			 * Value Anomaly 1h
-			 * TODO: Do more complex Anomaly Scenario
 			 */
-			System.out.println("Value Anomalies:");
-			float min = heaterDevice.getFSmin();
-			float max = heaterDevice.getFSmax();
-			float val = heaterDevice.getFSval();
-			heaterDevice.setFSmin(18);
-			heaterDevice.setFSval(18.5f);
-			heaterDevice.setFSmax(24);
+			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);
+			smartLight.setLabel((short)0);
+			
+			new_time = System.currentTimeMillis();
+			elapsed_time = new_time-currentTime;
+			System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
+			currentTime = new_time;
+			
+			/**
+			 * Schedule new light events
+			 */
+			eventTime = 27 * hour;
+			while(eventTime < 29 * hour) {
+				/**
+				 * At least 1 second between events
+				 */
+				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
+					
+					@Override
+					public void simulateEvent(long time) {
+						smartLight.setBSval(!smartLight.isTrueStatus());
+						smartLight.setTrueStatus(!smartLight.isTrueStatus());
+					}
+				});
+				
+				eventTime+=Math.max(1000, Math.round(lightDist.sample()));
+				
+			}
+			
+			/**
+			 * Temperature Anomaly 2h
+			 */
+			System.out.println("Temperature Anomaly:");
+			heaterDevice.setFSval(21f);
+			heaterDevice.setTrueTemperature(34f);
 			heaterDevice.setLabel((short)-1);//-1 Value anomaly
-			for(int i=25; i<26; i++)
+			for(int i=27; i<29; i++)
 				sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
-			heaterDevice.setFSmin(min);
-			heaterDevice.setFSmax(max);
-			heaterDevice.setFSval(val);
+			heaterDevice.setFSval(21f);
+			heaterDevice.setTrueTemperature(21f);
 			heaterDevice.setLabel((short)0);
 			
 			new_time = System.currentTimeMillis();