LightScenario.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar;
  2. import org.apache.commons.math3.distribution.NormalDistribution;
  3. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.PacketCaptureController;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.RoomStatus;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartLight;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartLightSensor;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureProducer;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureSensor;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
  22. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.AbstractEvent;
  23. import de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation.SWCKMeansClustering;
  24. import de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation.SWCKMeansClusteringWithLabels;
  25. public class LightScenario {
  26. Controller controller;
  27. NetworkController networkController;
  28. public LightScenario(Controller controller) {
  29. this.controller=controller;
  30. this.networkController=controller.getNetworkController();
  31. }
  32. /**
  33. * Creates the paper toy example, also simulates it, if run = true
  34. * @param run
  35. */
  36. @SuppressWarnings("unchecked")
  37. public void createSWCExample(boolean run){
  38. /**
  39. * Reset network, etc.
  40. */
  41. long hour = 60 * 60 * 1000;
  42. long endTime = 24 * hour;
  43. long noAnomEnd = endTime + 1 * hour;
  44. long lightEnd = noAnomEnd + 1 * hour;
  45. long stepLength = hour;
  46. long currentSimTime=0;
  47. SimulationController sim = controller.getSimulationController();
  48. if(run) {
  49. controller.getSimulationController().resetSimulation();
  50. controller.getNetworkController().deleteNetworkModel();
  51. /*
  52. * Simulate 24 hours
  53. */
  54. sim.setStartTime(0);
  55. sim.resetSimulation();
  56. sim.setStepDuration(hour);
  57. sim.setPrintPackets(false);
  58. sim.setEndTime(endTime);
  59. }
  60. /*
  61. * Main networking devices
  62. */
  63. SmartDevice smartHub = networkController.createSmartDevice("SmartHub", 400, 400, 50);
  64. /*
  65. * Links/Networks
  66. */
  67. Link zigbee = new PrecisionLink("ZigBee");
  68. networkController.addLink(zigbee);
  69. /*
  70. * Connect Devices to Links
  71. */
  72. networkController.addLinkToDevice(zigbee, smartHub);
  73. /*
  74. * Create MQTT Connection
  75. */
  76. Connection mqtt = new ConnectionPrecision();
  77. mqtt.setName("HomeAutomation (MQTT)");
  78. networkController.addConnectionToLink(mqtt, zigbee);
  79. mqtt.setProtocol(new MQTT_protocol());
  80. Port pBroker = new Port(smartHub, (short)1883);
  81. pBroker.setStatus(Port.OPEN);
  82. pBroker.setResponseTime((short)2);
  83. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  84. networkController.addConnection(mqtt);
  85. mqtt.setLabel((short) 0);
  86. /**
  87. * Room
  88. */
  89. String roomName = "Bedroom";
  90. RoomStatus room = new RoomStatus();
  91. room.setTemperature(19f);
  92. /*
  93. * Add some MQTT Devices
  94. */
  95. /*
  96. * Add light Sensor
  97. */
  98. SmartLightSensor lightSensor = new SmartLightSensor(roomName + " LightSensor", room, 1000);
  99. lightSensor.setBSinfoName("home/" + roomName + "/light");
  100. networkController.addLinkToDevice(zigbee, lightSensor);
  101. networkController.moveSmartDevice(lightSensor, 500, 300, 50);
  102. networkController.addSmartDevice(lightSensor);
  103. Port pLightSensor = new Port(lightSensor, (short)1883, 15000);
  104. pLightSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  105. pLightSensor.setStatus(Port.SENDING);
  106. pLightSensor.setLastTrigger(-1337L);
  107. networkController.addDeviceToConnectionAndProtocol(pLightSensor, mqtt,1);
  108. lightSensor.setLabel((short) 0);
  109. /*
  110. * Add a light
  111. */
  112. SmartLight smartLight = new SmartLight(roomName + " Light", room, lightSensor);
  113. smartLight.setBSinfoName("home/" + roomName + "/light");
  114. networkController.addLinkToDevice(zigbee, smartLight);
  115. networkController.moveSmartDevice(smartLight, 500, 500, 50);
  116. networkController.addSmartDevice(smartLight);
  117. Port pSmartLight = new Port(smartLight, (short)1883, 15000);
  118. pSmartLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  119. pSmartLight.setStatus(Port.SENDING);
  120. pSmartLight.setLastTrigger(-1207L);
  121. networkController.addDeviceToConnectionAndProtocol(pSmartLight, mqtt,1);
  122. smartLight.setLabel((short) 0);
  123. /*
  124. * Update visualization
  125. */
  126. controller.notifyObservers();
  127. /**
  128. * Run only if run == true
  129. */
  130. if(!run)return;
  131. try {
  132. System.out.println("Check 1");//TODO
  133. /**
  134. * Instances of the packet Sniffers
  135. */
  136. SWCKMeansClusteringWithLabels snifferKNNwithLabels = new SWCKMeansClusteringWithLabels();
  137. SWCKMeansClustering snifferKNNnoLabels = new SWCKMeansClustering();
  138. PacketCollector collectorKNNwithLabels = new PacketCollector(snifferKNNwithLabels);
  139. PacketCollector collectorKNNnoLabels = new PacketCollector(snifferKNNnoLabels);
  140. //PacketCollector collectorEM = new PacketCollector(snifferEM);
  141. //PacketCollector collectorHC = new PacketCollector(snifferHC);
  142. /*
  143. * Capture both links on all collectors
  144. */
  145. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  146. //captureController.addLinkToCollector(collectorEM, zigbee);
  147. //captureController.addLinkToCollector(collectorKNN, zigbee);
  148. captureController.addDeviceToCollector(collectorKNNwithLabels, smartLight);
  149. captureController.addPacketCollector(collectorKNNwithLabels);
  150. captureController.addDeviceToCollector(collectorKNNnoLabels, smartLight);
  151. captureController.addPacketCollector(collectorKNNnoLabels);
  152. //captureController.addPacketCollector(collectorEM);
  153. //captureController.addPacketCollector(collectorHC);
  154. System.out.println("Check 3: created Controller");//TODO
  155. long currentTime = System.currentTimeMillis();
  156. /**
  157. * Training events
  158. */
  159. System.out.println("Create training events");
  160. /**
  161. * Light Events (random switch every 30min+-15min
  162. */
  163. NormalDistribution lightDist = new NormalDistribution(hour/10, hour/20);
  164. long eventTime = 0;
  165. boolean on = true;
  166. while(eventTime < noAnomEnd) {
  167. /**
  168. * At least 1 second between events
  169. */
  170. final boolean newVal = on;
  171. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  172. @Override
  173. public void simulateEvent(long time) {
  174. smartLight.setTrueStatus(newVal);
  175. smartLight.setBSval(newVal);
  176. }
  177. });
  178. on = !on;
  179. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  180. }
  181. System.out.println("Training:");//TODO
  182. while(currentSimTime<endTime) {
  183. sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
  184. currentSimTime+=stepLength;
  185. }
  186. long new_time = System.currentTimeMillis();
  187. long elapsed_time = new_time-currentTime;
  188. System.out.println("Training data generation took: "+elapsed_time+"ms");
  189. currentTime = new_time;
  190. /*
  191. * Start Classifying
  192. */
  193. collectorKNNwithLabels.setMode(true);
  194. collectorKNNnoLabels.setMode(true);
  195. //collectorEM.setMode(true);
  196. //collectorHC.setMode(true);
  197. new_time = System.currentTimeMillis();
  198. elapsed_time = new_time-currentTime;
  199. System.out.println("Training of algorithm took: "+elapsed_time+"ms");
  200. currentTime = new_time;
  201. /*
  202. * Simulate/Test 2 hour without anomalies
  203. */
  204. snifferKNNwithLabels.setCurrentScenario("NoAnomalies");
  205. snifferKNNnoLabels.setCurrentScenario("NoAnomalies");
  206. System.out.println("Test w/0 anomalies:");//TODO
  207. sim.getSimulationManager().simulateTimeIntervall(currentSimTime, noAnomEnd-currentSimTime);
  208. currentSimTime=noAnomEnd;
  209. new_time = System.currentTimeMillis();
  210. elapsed_time = new_time-currentTime;
  211. System.out.println("Test witout anomalies took: "+elapsed_time+"ms");
  212. currentTime = new_time;
  213. /**
  214. * Light Anomaly 1h
  215. * Light != Sensor
  216. */
  217. snifferKNNwithLabels.setCurrentScenario("LightAnomalies");
  218. snifferKNNnoLabels.setCurrentScenario("LightAnomalies");
  219. System.out.println("Light Anomaly:");
  220. eventTime = noAnomEnd;
  221. on = true;
  222. smartLight.setTrueStatus(false);
  223. smartLight.setBSval(true);
  224. while(eventTime < lightEnd) {
  225. /**
  226. * At least 1 second between events
  227. */
  228. final boolean newVal = on;
  229. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  230. @Override
  231. public void simulateEvent(long time) {
  232. smartLight.setTrueStatus(newVal);
  233. smartLight.setBSval(!newVal);
  234. }
  235. });
  236. on = !on;
  237. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  238. }
  239. smartLight.setLabel((short)-1);
  240. sim.getSimulationManager().simulateTimeIntervall(currentSimTime, lightEnd-currentSimTime);
  241. currentSimTime=lightEnd;
  242. smartLight.setBSval(smartLight.isTrueStatus());
  243. smartLight.setLabel((short)0);
  244. new_time = System.currentTimeMillis();
  245. elapsed_time = new_time-currentTime;
  246. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  247. currentTime = new_time;
  248. System.out.println("Testing completed");
  249. } catch(Exception e) {
  250. System.out.println("WARNING: Testing failed: ");
  251. e.printStackTrace();
  252. }
  253. }
  254. }