|
@@ -0,0 +1,287 @@
|
|
|
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar;
|
|
|
+
|
|
|
+import org.apache.commons.math3.distribution.NormalDistribution;
|
|
|
+
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.PacketCaptureController;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
|
|
|
+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.SmartLight;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartLightSensor;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureProducer;
|
|
|
+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.evaluation.SWCKMeansClustering;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation.SWCKMeansClusteringWithLabels;
|
|
|
+
|
|
|
+public class HeaterScenarioFakeData {
|
|
|
+
|
|
|
+ Controller controller;
|
|
|
+ NetworkController networkController;
|
|
|
+ public HeaterScenarioFakeData(Controller controller, NetworkController networkController) {
|
|
|
+ this.controller=controller;
|
|
|
+ this.networkController=networkController;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates the paper toy example, also simulates it, if run = true
|
|
|
+ * @param run
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public void createSWCExample(boolean run){
|
|
|
+ /**
|
|
|
+ * Reset network, etc.
|
|
|
+ */
|
|
|
+
|
|
|
+ long hour = 60 * 60 * 1000;
|
|
|
+ long endTime = 24 * hour;
|
|
|
+ long noAnomEnd = endTime + 1 * hour;
|
|
|
+ long heaterEnd = noAnomEnd + 1 * 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
|
|
|
+ */
|
|
|
+ SmartDevice smartHub = networkController.createSmartDevice("SmartHub", 400, 400, 50);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Links/Networks
|
|
|
+ */
|
|
|
+ Link zigbee = new PrecisionLink("ZigBee");
|
|
|
+ networkController.addLink(zigbee);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Connect Devices to Links
|
|
|
+ */
|
|
|
+ networkController.addLinkToDevice(zigbee, smartHub);
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Create MQTT Connection
|
|
|
+ */
|
|
|
+ Connection mqtt = new ConnectionPrecision();
|
|
|
+ mqtt.setName("HomeAutomation (MQTT)");
|
|
|
+ networkController.addConnectionToLink(mqtt, zigbee);
|
|
|
+ mqtt.setProtocol(new MQTT_protocol());
|
|
|
+ Port pBroker = new Port(smartHub, (short)1883);
|
|
|
+ pBroker.setStatus(Port.OPEN);
|
|
|
+ pBroker.setResponseTime((short)2);
|
|
|
+ networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
|
|
|
+ networkController.addConnection(mqtt);
|
|
|
+ mqtt.setLabel((short) 0);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Room
|
|
|
+ */
|
|
|
+ String roomName = "Bedroom";
|
|
|
+ RoomStatus room = new RoomStatus();
|
|
|
+ room.setTemperature(19f);
|
|
|
+ /*
|
|
|
+ * Add some MQTT Devices
|
|
|
+ */
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Room Thermostat
|
|
|
+ */
|
|
|
+ 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)
|
|
|
+ */
|
|
|
+ SmartTemperatureProducer heaterDevice = new SmartTemperatureProducer(roomName + " Heater", room, 500, heatSensor);
|
|
|
+ heaterDevice.setFSinfoName("home/" + roomName + "/heaterTemp");
|
|
|
+ networkController.addLinkToDevice(zigbee, heaterDevice);
|
|
|
+ networkController.moveSmartDevice(heaterDevice, 300, 500, 50);
|
|
|
+ heaterDevice.setFSmin(19.0f);
|
|
|
+ heaterDevice.setFSmax(21.0f);
|
|
|
+ heaterDevice.setFSval(19.0f);
|
|
|
+ networkController.addSmartDevice(heaterDevice);
|
|
|
+ Port pHeaterDevice = new Port(heaterDevice, (short)1883, 15000);
|
|
|
+ pHeaterDevice.setStatus(Port.SENDING);
|
|
|
+ pHeaterDevice.setTriggerHandler(new NormalDistributionHandler(15000, 500));
|
|
|
+ pHeaterDevice.setLastTrigger(-1231L);
|
|
|
+ networkController.addDeviceToConnectionAndProtocol(pHeaterDevice, mqtt,1);
|
|
|
+ heaterDevice.setLabel((short) 0);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Update visualization
|
|
|
+ */
|
|
|
+ controller.notifyObservers();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Run only if run == true
|
|
|
+ */
|
|
|
+ if(!run)return;
|
|
|
+ try {
|
|
|
+ System.out.println("Check 1");//TODO
|
|
|
+ /**
|
|
|
+ * Instances of the packet Sniffers
|
|
|
+ */
|
|
|
+ SWCKMeansClusteringWithLabels snifferKNNwithLabels = new SWCKMeansClusteringWithLabels();
|
|
|
+ SWCKMeansClustering snifferKNNnoLabels = new SWCKMeansClustering();
|
|
|
+
|
|
|
+
|
|
|
+ PacketCollector collectorKNNwithLabels = new PacketCollector(snifferKNNwithLabels);
|
|
|
+ PacketCollector collectorKNNnoLabels = new PacketCollector(snifferKNNnoLabels);
|
|
|
+ //PacketCollector collectorEM = new PacketCollector(snifferEM);
|
|
|
+ //PacketCollector collectorHC = new PacketCollector(snifferHC);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Capture both links on all collectors
|
|
|
+ */
|
|
|
+ PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
|
|
|
+ //captureController.addLinkToCollector(collectorEM, zigbee);
|
|
|
+ //captureController.addLinkToCollector(collectorKNN, zigbee);
|
|
|
+ captureController.addDeviceToCollector(collectorKNNwithLabels, heaterDevice);
|
|
|
+ captureController.addPacketCollector(collectorKNNwithLabels);
|
|
|
+ captureController.addDeviceToCollector(collectorKNNnoLabels, heaterDevice);
|
|
|
+ captureController.addPacketCollector(collectorKNNnoLabels);
|
|
|
+ //captureController.addPacketCollector(collectorEM);
|
|
|
+ //captureController.addPacketCollector(collectorHC);
|
|
|
+
|
|
|
+ System.out.println("Check 3: created Controller");//TODO
|
|
|
+
|
|
|
+ long currentTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Training events
|
|
|
+ */
|
|
|
+ System.out.println("Create training events");
|
|
|
+ /**
|
|
|
+ * Heater Events (random switch every 2h +- 30min
|
|
|
+ */
|
|
|
+ NormalDistribution tempDist = new NormalDistribution(2*hour, hour/2);
|
|
|
+ long eventTime = 0;
|
|
|
+ boolean low = true;
|
|
|
+ while(eventTime < noAnomEnd) {
|
|
|
+ /**
|
|
|
+ * At least 1 second between events
|
|
|
+ */
|
|
|
+ final float newTemp;
|
|
|
+ if(low)
|
|
|
+ newTemp = heaterDevice.getFSmax();
|
|
|
+ else
|
|
|
+ newTemp = heaterDevice.getFSmin();
|
|
|
+ low = !low;
|
|
|
+ SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void simulateEvent(long time) {
|
|
|
+ heaterDevice.setFSval(newTemp);
|
|
|
+ heaterDevice.setTrueTemperature(newTemp);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ eventTime+=Math.max(1000, Math.round(tempDist.sample()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("Training:");//TODO
|
|
|
+ while(currentSimTime<endTime) {
|
|
|
+ sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
|
|
|
+ currentSimTime+=stepLength;
|
|
|
+ }
|
|
|
+
|
|
|
+ long new_time = System.currentTimeMillis();
|
|
|
+ long elapsed_time = new_time-currentTime;
|
|
|
+ System.out.println("Training data generation took: "+elapsed_time+"ms");
|
|
|
+ currentTime = new_time;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Start Classifying
|
|
|
+ */
|
|
|
+ collectorKNNwithLabels.setMode(true);
|
|
|
+ collectorKNNnoLabels.setMode(true);
|
|
|
+ //collectorEM.setMode(true);
|
|
|
+ //collectorHC.setMode(true);
|
|
|
+ new_time = System.currentTimeMillis();
|
|
|
+ elapsed_time = new_time-currentTime;
|
|
|
+ System.out.println("Training of algorithm took: "+elapsed_time+"ms");
|
|
|
+ currentTime = new_time;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Simulate/Test 2 hour without anomalies
|
|
|
+ */
|
|
|
+ snifferKNNwithLabels.setCurrentScenario("NoAnomalies");
|
|
|
+ snifferKNNnoLabels.setCurrentScenario("NoAnomalies");
|
|
|
+ System.out.println("Test w/0 anomalies:");//TODO
|
|
|
+ sim.getSimulationManager().simulateTimeIntervall(currentSimTime, noAnomEnd-currentSimTime);
|
|
|
+ currentSimTime=noAnomEnd;
|
|
|
+
|
|
|
+
|
|
|
+ new_time = System.currentTimeMillis();
|
|
|
+ elapsed_time = new_time-currentTime;
|
|
|
+ System.out.println("Test witout anomalies took: "+elapsed_time+"ms");
|
|
|
+ currentTime = new_time;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Temperature Anomaly 1h
|
|
|
+ */
|
|
|
+ snifferKNNwithLabels.setCurrentScenario("TemperatureAnomalies");
|
|
|
+ snifferKNNnoLabels.setCurrentScenario("TemperatureAnomalies");
|
|
|
+ System.out.println("Temperature Anomaly:");
|
|
|
+ heaterDevice.setFSval(34f);
|
|
|
+ heaterDevice.setTrueTemperature(21f);
|
|
|
+ heaterDevice.setLabel((short)-1);//-1 Value anomaly
|
|
|
+
|
|
|
+ sim.getSimulationManager().simulateTimeIntervall(currentSimTime, heaterEnd-currentSimTime);
|
|
|
+ currentSimTime=heaterEnd;
|
|
|
+
|
|
|
+ heaterDevice.setFSval(21f);
|
|
|
+ heaterDevice.setTrueTemperature(21f);
|
|
|
+ heaterDevice.setLabel((short)0);
|
|
|
+
|
|
|
+ new_time = System.currentTimeMillis();
|
|
|
+ elapsed_time = new_time-currentTime;
|
|
|
+ System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
|
|
|
+ currentTime = new_time;
|
|
|
+
|
|
|
+ System.out.println("Testing completed");
|
|
|
+ } catch(Exception e) {
|
|
|
+ System.out.println("WARNING: Testing failed: ");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|