MenuBarNetworkExamples.java 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.Map.Entry;
  7. import javax.swing.JMenu;
  8. import javax.swing.JMenuItem;
  9. import org.apache.commons.math3.distribution.NormalDistribution;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ExampleAnomalyController;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ImportController;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.PacketCaptureController;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SettingsController;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPerformance;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  22. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  23. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
  24. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  25. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
  26. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  27. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.RoomStatus;
  28. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
  29. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  30. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolCollectorDevice;
  31. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensorDevice;
  32. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatCollectorDevice;
  33. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensorDevice;
  34. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartLight;
  35. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartLightSensor;
  36. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureProducer;
  37. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureSensor;
  38. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
  39. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
  40. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.AbstractEvent;
  41. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.CountingMetric;
  42. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.Manipulation_RandomMove;
  43. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  44. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
  45. import de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation.SWCKMeansClustering;
  46. import de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation.SWCKMeansClusteringWithLabels;
  47. public class MenuBarNetworkExamples extends JMenu{
  48. /**
  49. *
  50. */
  51. private static final long serialVersionUID = 1L;
  52. /**
  53. * Controller controller
  54. */
  55. private Controller controller;
  56. /**
  57. * Controller for editing the network
  58. */
  59. private NetworkController networkController;
  60. /**
  61. * Controller to insert anomalies
  62. */
  63. private ExampleAnomalyController anomalyController;
  64. /**
  65. * Controller for configuration of the program
  66. */
  67. private SettingsController settingsController;
  68. /**
  69. * SimulationController for running the simulation
  70. */
  71. private SimulationController simulationController;
  72. /**
  73. * Creates the JMenu for network examples in the MenuBar
  74. * @param controller controller to create examples
  75. */
  76. public MenuBarNetworkExamples(Controller controller) {
  77. super("Create example Network");
  78. this.controller = controller;
  79. this.networkController = controller.getNetworkController();
  80. this.anomalyController = networkController.getAnomalyController();
  81. this.settingsController = controller.getSettingsController();
  82. this.simulationController = controller.getSimulationController();
  83. /**
  84. * MQTT example
  85. */
  86. JMenuItem mnMQTT = new JMenuItem("MQTT Example");
  87. mnMQTT.addActionListener(a->initializeMQTTTest());
  88. this.add(mnMQTT);
  89. /**
  90. * Small Network Test
  91. */
  92. JMenuItem mnTest = new JMenuItem("Network Example");
  93. mnTest.addActionListener(a->initializeTest());
  94. this.add(mnTest);
  95. /**
  96. * Performance Comparison Example
  97. */
  98. JMenuItem mnPerformance = new JMenuItem("Performane Evaluation Preset");
  99. mnPerformance.addActionListener(a->initializePerformanceTest());
  100. this.add(mnPerformance);
  101. /**
  102. * User Study Preset
  103. */
  104. JMenuItem mnUserStudy = new JMenuItem("User Study Preset");
  105. mnUserStudy.addActionListener(a->initializeUserStudy());
  106. this.add(mnUserStudy);
  107. /**
  108. * Packet Collection example
  109. */
  110. JMenuItem mnPacketCollectionExmample = new JMenuItem("Packet Collection Example");
  111. mnPacketCollectionExmample.addActionListener(a->testPackageCollection());
  112. this.add(mnPacketCollectionExmample);
  113. /**
  114. * Paper example
  115. */
  116. JMenuItem mnPaperExample = new JMenuItem("Paper Example");
  117. mnPaperExample.addActionListener(a->createPaperExample(false));
  118. this.add(mnPaperExample);
  119. /**
  120. * Paper example + run
  121. */
  122. JMenuItem mnRunPaperExample = new JMenuItem("Run Paper Example");
  123. mnRunPaperExample.addActionListener(a->createPaperExample(true));
  124. this.add(mnRunPaperExample);
  125. /**
  126. * SWC example
  127. */
  128. JMenuItem mnSWCExample = new JMenuItem("SWC Example");
  129. mnSWCExample.addActionListener(a->createSWCExample(false));
  130. this.add(mnSWCExample);
  131. /**
  132. * SWC example + run
  133. */
  134. JMenuItem mnRunSWCExample = new JMenuItem("Run SWC Example");
  135. mnRunSWCExample.addActionListener(a->createSWCExample(true));
  136. this.add(mnRunSWCExample);
  137. }
  138. private void initializePerformanceTest(){
  139. SmartDevice tic = new SmartDevice("Tic");
  140. tic.setX(100);
  141. tic.setY(100);
  142. SmartDevice toc = new SmartDevice("Toc");
  143. toc.setX(100);
  144. toc.setY(250);
  145. networkController.addSmartDevice(tic);
  146. networkController.addSmartDevice(toc);
  147. Link l = new PrecisionLink("Channel");
  148. networkController.addLink(l);
  149. networkController.addLinkToDevice(l, tic);
  150. networkController.addLinkToDevice(l, toc);
  151. Port ticP = new Port(tic, (short)2, 200, (short)0, 0, (short)0);
  152. Port tocP = new Port(toc, (short)3, 200, (short)0, -100, (short)0);
  153. tic.addPort(ticP);
  154. toc.addPort(tocP);
  155. Connection con = new ConnectionPrecision(l, new SimpleProtocol());
  156. networkController.addConnection(con);
  157. networkController.addConnectionToLink(con, l);
  158. networkController.addDeviceToConnectionAndProtocol(ticP, con, 0);
  159. networkController.addDeviceToConnectionAndProtocol(tocP, con, 1);
  160. simulationController.setStepDuration(100000000);
  161. simulationController.setEndTime(1000000000);
  162. controller.notifyObservers();
  163. }
  164. /**
  165. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  166. */
  167. private void initializeUserStudy(){
  168. /**
  169. * Preset one SmartDevice
  170. */
  171. SmartDevice A = new SmartDevice("Homestation");
  172. A.setX(200);
  173. A.setY(200);
  174. networkController.addSmartDevice(A);
  175. /**
  176. * Packagecollector, which is registered at the Device
  177. */
  178. PacketCollector collector = new PacketCollector(new CountingMetric());
  179. simulationController.getPacketCaptureController().addPacketCollector(collector);
  180. simulationController.getPacketCaptureController().addDeviceToCollector(collector, A);
  181. simulationController.setPrintPackets(true);
  182. controller.notifyObservers();
  183. }
  184. /**
  185. * Test package collectors
  186. */
  187. private void testPackageCollection() {
  188. if(networkController.getSmartDevices().size()<3)
  189. return;
  190. simulationController.addAlgorithm(new Manipulation_RandomMove(), controller);
  191. PacketCollector collector = new PacketCollector();
  192. simulationController.getSimulationManager().getPacketCollectionManager().addPacketCollector(collector);
  193. System.out.println("Collector 0-500 - nothing collected:");
  194. simulationController.getSimulationManager().simulateTimeIntervall(0, 500);
  195. HashMap<Link, LinkedList<Packet>> map = collector.getPackets();
  196. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  197. System.out.println("Link: "+e.getKey().getName());
  198. for(Packet p : e.getValue())
  199. System.out.println(p.getTextualRepresentation());
  200. }
  201. System.out.println("");
  202. System.out.println("");
  203. Iterator<SmartDevice> it = networkController.getSmartDevices().iterator();
  204. it.next();
  205. SmartDevice d = it.next();
  206. collector.addDevice(d);
  207. SmartDevice f = it.next();
  208. collector.addDevice(f);
  209. System.out.println("Collector 500-1000 - "+d.getName()+" & "+f.getName()+" collected:");
  210. simulationController.getSimulationManager().simulateTimeIntervall(500, 500);
  211. map = collector.getPackets();
  212. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  213. System.out.println("Link: "+e.getKey().getName());
  214. for(Packet p : e.getValue())
  215. System.out.println(p.getTextualRepresentation());
  216. }
  217. System.out.println("");
  218. System.out.println("");
  219. Link l = networkController.getLinks().iterator().next();
  220. collector.addLink(l);
  221. System.out.println("Collector 2000-3000 - "+l+" collected:");
  222. simulationController.getSimulationManager().simulateTimeIntervall(1000, 500);
  223. map = collector.getPackets();
  224. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  225. System.out.println("Link: "+e.getKey().getName());
  226. for(Packet p : e.getValue())
  227. System.out.println(p.getTextualRepresentation());
  228. }
  229. simulationController.resetSimulation();
  230. controller.notifyObservers();
  231. }
  232. /**
  233. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  234. */
  235. private void initializeTest(){
  236. SmartDevice A = null, B = null, C = null;
  237. for(int i = 0; i<5; i++){
  238. A = new SmartDevice("SmartTV"+i);
  239. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  240. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  241. B = new SmartDevice("SmartDoor"+i);
  242. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  243. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  244. C = new SmartDevice("SmartLight"+i);
  245. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  246. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  247. networkController.addSmartDevice(A);
  248. networkController.addSmartDevice(B);
  249. networkController.addSmartDevice(C);
  250. }
  251. Link link = new SimpleLink("SimpleWifi");
  252. link.addDevice(A);
  253. link.addDevice(B);
  254. link.addDevice(C);
  255. A.addLink(link);
  256. B.addLink(link);
  257. C.addLink(link);
  258. Port a = new Port(A, (short) 1, 68);
  259. a.setLastTrigger(0);
  260. a.setStatus(Port.SENDING);
  261. A.addPort(a);
  262. Port b = new Port(B, (short) 2, 102);
  263. b.setStatus(Port.SENDING);
  264. B.addPort(b);
  265. Connection s = new ConnectionPerformance(link, new SimpleProtocol(a, b));
  266. s.setPacketLossProbability(0.01);//1% Packet loss probability
  267. a.setConnection(s);
  268. b.setConnection(s);
  269. s.addSmartDevice(a);
  270. s.addSmartDevice(b);
  271. link.addConnection(s);
  272. networkController.addLink(link);
  273. networkController.addConnection(s);
  274. controller.notifyObservers();
  275. }
  276. /**
  277. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  278. */
  279. private void initializeMQTTTest(){
  280. SimpleLink link = new SimpleLink("WIFI");
  281. link.setFixedDelay(5);
  282. SmartDevice broker = new SmartDevice("MQTT-Broker");
  283. broker.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  284. broker.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  285. networkController.addSmartDevice(broker);
  286. Port brokerPort = new Port(broker, (short) 0, 987);
  287. brokerPort.setLastTrigger(0);
  288. brokerPort.setStatus(Port.OPEN);
  289. broker.addPort(brokerPort);
  290. link.addDevice(broker);
  291. broker.addLink(link);
  292. Protocol protocol = new MQTT_protocol(brokerPort);
  293. Connection con = new ConnectionPrecision(link, protocol);
  294. con.setPacketLossProbability(0.01);//1% Packet loss probability
  295. con.addSmartDevice(brokerPort);
  296. brokerPort.setConnection(con);
  297. con.setStatus(Connection.ACTIVE);
  298. networkController.addLink(link);
  299. link.addConnection(con);
  300. networkController.addConnection(con);
  301. SmartDevice A = null, A1 = null, A2 = null, B = null, C = null;
  302. Port aP,a1P,a2P,bP,cP;
  303. for(int i = 0; i<3; i++){
  304. FloatSensorDevice aS = new FloatSensorDevice("SmartThermostat"+i+"(Pub)");
  305. aS.setFSinfoName("room"+i+"/temperature");
  306. A=aS;
  307. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  308. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  309. link.addDevice(A);
  310. A.addLink(link);
  311. aP = new Port(A,((short) (3*i+1)),100+(int)(Math.random()*900));
  312. aP.setLastTrigger(0);
  313. aP.setJitter((short) (Math.random()*50));
  314. aP.setStatus(Port.SENDING);
  315. aP.setConnection(con);
  316. protocol.addDeviceOfRole(aP, 2);
  317. con.addSmartDevice(aP);
  318. A.addPort(aP);
  319. networkController.addSmartDevice(A);
  320. FloatCollectorDevice aS1 = new FloatCollectorDevice("SmartAirCondition"+i+"(Sub)");
  321. aS1.setFCinfoName("room"+i+"/temperature");
  322. A1=aS1;
  323. A1.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  324. A1.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  325. link.addDevice(A1);
  326. A1.addLink(link);
  327. a1P = new Port(A1,((short) (3*i+1)),100+(int)(Math.random()*900));
  328. a1P.setLastTrigger(0);
  329. a1P.setJitter((short) (Math.random()*50));
  330. a1P.setStatus(Port.SENDING);
  331. a1P.setConnection(con);
  332. protocol.addDeviceOfRole(a1P, 3);
  333. con.addSmartDevice(a1P);
  334. A1.addPort(a1P);
  335. networkController.addSmartDevice(A1);
  336. FloatCollectorDevice aS2 = new FloatCollectorDevice("SmartHeater"+i+"(Sub)");
  337. aS2.setFCinfoName("room"+i+"/temperature");
  338. A2=aS2;
  339. A2.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  340. A2.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  341. link.addDevice(A2);
  342. A2.addLink(link);
  343. a2P = new Port(A2,((short) (3*i+1)),100+(int)(Math.random()*900));
  344. a2P.setLastTrigger(0);
  345. a2P.setJitter((short) (Math.random()*50));
  346. a2P.setStatus(Port.SENDING);
  347. a2P.setConnection(con);
  348. protocol.addDeviceOfRole(a2P, 3);
  349. con.addSmartDevice(a2P);
  350. A2.addPort(a2P);
  351. networkController.addSmartDevice(A2);
  352. B = new BoolSensorDevice("SmartDoor"+i+"(Pub)");
  353. ((BoolSensorDevice)B).setBSinfoName("room"+i+"/DoorOpen");
  354. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  355. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  356. link.addDevice(B);
  357. B.addLink(link);
  358. bP = new Port(B,((short) (3*i+2)),10+(int)(Math.random()*190));
  359. bP.setLastTrigger(0);
  360. bP.setJitter((short) (Math.random()*50));
  361. bP.setStatus(Port.SENDING);
  362. bP.setConnection(con);
  363. protocol.addDeviceOfRole(bP, 2);
  364. con.addSmartDevice(bP);
  365. B.addPort(bP);
  366. networkController.addSmartDevice(B);
  367. C = new BoolCollectorDevice("SmartDoorStatusLight"+i+"(Sub)");
  368. ((BoolCollectorDevice)C).setBCinfoName("room"+i+"/DoorOpen");
  369. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  370. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  371. link.addDevice(C);
  372. C.addLink(link);
  373. cP = new Port(C,((short) (3*i+1)), 50+(int)(Math.random()*450));
  374. cP.setLastTrigger(0);
  375. cP.setJitter((short) (Math.random()*50));
  376. cP.setStatus(Port.SENDING);
  377. cP.setConnection(con);
  378. protocol.addDeviceOfRole(cP, 3);
  379. con.addSmartDevice(cP);
  380. C.addPort(cP);
  381. networkController.addSmartDevice(C);
  382. controller.notifyObservers();
  383. }
  384. }
  385. /**
  386. * Creates the paper toy example, also simulates it, if run = true
  387. * @param run
  388. */
  389. @SuppressWarnings("unchecked")
  390. public void createPaperExample(boolean run){
  391. /**
  392. * Reset network, etc.
  393. */
  394. if(run) {
  395. controller.getSimulationController().resetSimulation();
  396. controller.getNetworkController().deleteNetworkModel();
  397. }
  398. /*
  399. * Main networking devices
  400. */
  401. SmartDevice router = networkController.createSmartDevice("Wifi-Router", 500, 100, 50);
  402. SmartDevice zigBeeRouter = networkController.createSmartDevice("ZigBee-Router", 500, 300, 50);
  403. SmartDevice broker = networkController.createSmartDevice("SmartHub", 500, 500, 50);
  404. /*
  405. * Links/Networks
  406. */
  407. Link wifi = new PrecisionLink("Wifi");
  408. networkController.addLink(wifi);
  409. Link zigbee = new PrecisionLink("ZigBee");
  410. networkController.addLink(zigbee);
  411. /*
  412. * Connect Devices to Links
  413. */
  414. networkController.addLinkToDevice(wifi, router);
  415. networkController.addLinkToDevice(wifi, zigBeeRouter);
  416. networkController.addLinkToDevice(zigbee, zigBeeRouter);
  417. networkController.addLinkToDevice(zigbee, broker);
  418. /*
  419. * Internet Access Connection
  420. */
  421. Connection inetAcces = new ConnectionPrecision();
  422. inetAcces.setName("Cloud Access");
  423. networkController.addConnectionToLink(inetAcces, wifi);
  424. inetAcces.setProtocol(new SimpleProtocol());
  425. Port pRouter = new Port(router, (short)80, 500L);
  426. pRouter.setTriggerHandler(new NormalDistributionHandler(500, 100));
  427. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  428. Port pZigBee = new Port(zigBeeRouter, (short)80, 1000L);
  429. pZigBee.setTriggerHandler(new NormalDistributionHandler(1000, 300));
  430. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  431. networkController.addDeviceToConnectionAndProtocol(pZigBee, inetAcces, 1);
  432. networkController.addConnection(inetAcces);
  433. /*
  434. * ZigbeeRouter -> Broker ?
  435. */
  436. Connection homeAutomationInternetAccess = new ConnectionPrecision();
  437. homeAutomationInternetAccess.setName("Home Automation Webinterface");
  438. networkController.addConnectionToLink(homeAutomationInternetAccess, zigbee);
  439. homeAutomationInternetAccess.setProtocol(new SimpleProtocol());
  440. Port pBrokerWebInterface = new Port(broker, (short)80);
  441. pBrokerWebInterface.setTriggerHandler(new NormalDistributionHandler(2000, 500));
  442. pBrokerWebInterface.setStatus(Port.SENDING);
  443. pBrokerWebInterface.setResponseTime((short)2);
  444. pBrokerWebInterface.setLastTrigger(-284L);
  445. networkController.addDeviceToConnectionAndProtocol(pBrokerWebInterface, homeAutomationInternetAccess, 0);
  446. networkController.addConnection(homeAutomationInternetAccess);
  447. Port pRouterWebInterface = new Port(zigBeeRouter, (short)80);
  448. pRouterWebInterface.setTriggerHandler(new NormalDistributionHandler(5000, 3000));
  449. pRouterWebInterface.setStatus(Port.SENDING);
  450. pRouterWebInterface.setResponseTime((short)2);
  451. pRouterWebInterface.setLastTrigger(-142L);
  452. networkController.addDeviceToConnectionAndProtocol(pRouterWebInterface, homeAutomationInternetAccess, 1);
  453. networkController.addConnection(homeAutomationInternetAccess);
  454. /*
  455. * Create MQTT Connection
  456. */
  457. Connection mqtt = new ConnectionPrecision();
  458. mqtt.setName("Automation (MQTT)");
  459. networkController.addConnectionToLink(mqtt, zigbee);
  460. mqtt.setProtocol(new MQTT_protocol());
  461. Port pBroker = new Port(broker, (short)1883);
  462. pBroker.setStatus(Port.OPEN);
  463. pBroker.setResponseTime((short)2);
  464. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  465. networkController.addConnection(mqtt);
  466. /*
  467. * Add some MQTT Devices
  468. */
  469. /**
  470. * Kitchen Thermostat
  471. */
  472. FloatSensorDevice floatSensor = new FloatSensorDevice("Kitchen Thermostat");
  473. floatSensor.setFSinfoName("home/kitchen/temperature");
  474. networkController.addLinkToDevice(zigbee, floatSensor);
  475. networkController.moveSmartDevice(floatSensor, 300, 500, 50);
  476. floatSensor.setFSmin(15.0f);
  477. floatSensor.setFSmax(32.0f);
  478. networkController.addSmartDevice(floatSensor);
  479. Port pFloatSensor = new Port(floatSensor, (short)1883, 15000);
  480. pFloatSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  481. pFloatSensor.setStatus(Port.SENDING);
  482. pFloatSensor.setLastTrigger(-357L);
  483. networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
  484. /*
  485. * Add Fridge
  486. */
  487. FloatSensorDevice kitchenFridge = new FloatSensorDevice("Smart Fridge");
  488. kitchenFridge.setFSinfoName("home/kitchen/fridgeTemp");
  489. networkController.addLinkToDevice(zigbee, kitchenFridge);
  490. networkController.moveSmartDevice(kitchenFridge, 100, 600, 50);
  491. kitchenFridge.setFSmin(-6.0f);
  492. kitchenFridge.setFSmax(-4.0f);
  493. networkController.addSmartDevice(kitchenFridge);
  494. Port pKitchenFridge = new Port(kitchenFridge, (short)1883, 15000);
  495. pKitchenFridge.setStatus(Port.SENDING);
  496. pKitchenFridge.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  497. pKitchenFridge.setLastTrigger(-1231L);
  498. networkController.addDeviceToConnectionAndProtocol(pKitchenFridge, mqtt,1);
  499. /*
  500. * Add some kitchen lights
  501. */
  502. BoolSensorDevice kitchenLight = new BoolSensorDevice("Kitchen Light");
  503. kitchenLight.setBSinfoName("home/kitchen/light");
  504. networkController.addLinkToDevice(zigbee, kitchenLight);
  505. networkController.moveSmartDevice(kitchenLight, 250, 400, 50);
  506. networkController.addSmartDevice(kitchenLight);
  507. Port pKitchenLight = new Port(kitchenLight, (short)1883, 15000);
  508. pKitchenLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  509. pKitchenLight.setStatus(Port.SENDING);
  510. pKitchenLight.setLastTrigger(-1207L);
  511. networkController.addDeviceToConnectionAndProtocol(pKitchenLight, mqtt,1);
  512. //TODO Further devices & Connections
  513. /*
  514. * Bedroom
  515. */
  516. BoolSensorDevice sleepingRoomLight = new BoolSensorDevice("Bedroom Light");
  517. sleepingRoomLight.setBSinfoName("home/bedroom/light");
  518. networkController.addLinkToDevice(zigbee, sleepingRoomLight);
  519. networkController.moveSmartDevice(sleepingRoomLight, 750, 400, 50);
  520. networkController.addSmartDevice(sleepingRoomLight);
  521. Port pBedroomLight = new Port(sleepingRoomLight, (short)1883, 15000);
  522. pBedroomLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  523. pBedroomLight.setStatus(Port.SENDING);
  524. pBedroomLight.setLastTrigger(-1337L);
  525. networkController.addDeviceToConnectionAndProtocol(pBedroomLight, mqtt,1);
  526. /*
  527. * Bedroom Thermostat
  528. */
  529. FloatSensorDevice bedroomThermostat = new FloatSensorDevice("Bedroom Thermostat");
  530. bedroomThermostat.setFSinfoName("home/bedroom/temperature");
  531. networkController.addLinkToDevice(zigbee, bedroomThermostat);
  532. networkController.moveSmartDevice(bedroomThermostat, 700, 500, 50);
  533. bedroomThermostat.setFSmin(15.0f);
  534. bedroomThermostat.setFSmax(32.0f);
  535. networkController.addSmartDevice(bedroomThermostat);
  536. Port pBedroomThermostat = new Port(bedroomThermostat, (short)1883, 15000);
  537. pBedroomThermostat.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  538. pBedroomThermostat.setStatus(Port.SENDING);
  539. pBedroomThermostat.setLastTrigger(-820L);
  540. networkController.addDeviceToConnectionAndProtocol(pBedroomThermostat, mqtt,1);
  541. /*
  542. * Bedroom Info Screen
  543. */
  544. FloatCollectorDevice bedroomInfoScreen = new FloatCollectorDevice("Information Panel");
  545. bedroomInfoScreen.setFCinfoName("home/kitchen/fridgeTemp");
  546. networkController.addLinkToDevice(zigbee, bedroomInfoScreen);
  547. networkController.moveSmartDevice(bedroomInfoScreen, 900, 600, 50);
  548. networkController.addSmartDevice(bedroomInfoScreen);
  549. Port pBedroomInfo = new Port(bedroomInfoScreen, (short)1883, 15000);
  550. pBedroomInfo.setStatus(Port.SENDING);
  551. pBedroomInfo.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  552. pBedroomInfo.setLastTrigger(-666L);
  553. networkController.addDeviceToConnectionAndProtocol(pBedroomInfo, mqtt,1);
  554. /*
  555. * Update visualization
  556. */
  557. controller.notifyObservers();
  558. /**
  559. * Run only if run == true
  560. */
  561. if(!run)return;
  562. try {
  563. System.out.println("Check 1");//TODO
  564. /**
  565. * Instances of the packet Sniffers
  566. */
  567. PacketSniffer snifferEM = null, snifferKNN = null, snifferHC = null;
  568. /*
  569. * Import Example PacketSniffer algorithms
  570. */
  571. Class<? extends PacketSniffer> em = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/EMClustering.java"));
  572. snifferEM = em.newInstance();
  573. Class<? extends PacketSniffer> knn = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/KMeansClustering.java"));
  574. snifferKNN = knn.newInstance();
  575. Class<? extends PacketSniffer> hc = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/HierarchicalClustering.java"));
  576. snifferHC = hc.newInstance();
  577. System.out.println("Check 2: Imported");//TODO
  578. /*
  579. * Create collectors
  580. */
  581. PacketCollector collectorKNN = new PacketCollector(snifferKNN);
  582. //PacketCollector collectorEM = new PacketCollector(snifferEM);
  583. //PacketCollector collectorHC = new PacketCollector(snifferHC);
  584. /*
  585. * Capture both links on all collectors
  586. */
  587. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  588. //captureController.addLinkToCollector(collectorEM, zigbee);
  589. //captureController.addLinkToCollector(collectorEM, wifi);
  590. captureController.addLinkToCollector(collectorKNN, zigbee);
  591. captureController.addLinkToCollector(collectorKNN, wifi);
  592. //captureController.addLinkToCollector(collectorHC, zigbee);
  593. //captureController.addLinkToCollector(collectorHC, wifi);
  594. captureController.addPacketCollector(collectorKNN);
  595. //captureController.addPacketCollector(collectorEM);
  596. //captureController.addPacketCollector(collectorHC);
  597. System.out.println("Check 3: created Controller");//TODO
  598. long currentTime = System.currentTimeMillis();
  599. long hour = 60 * 60 * 1000;
  600. /*
  601. * Simulate 24 hours
  602. */
  603. SimulationController sim = controller.getSimulationController();
  604. sim.setStartTime(0);
  605. sim.resetSimulation();
  606. sim.setStepDuration(hour);
  607. sim.setPrintPackets(false);
  608. sim.setEndTime(24*hour);
  609. kitchenFridge.setFSmin(-6f);
  610. kitchenFridge.setFSmax(-4f);
  611. kitchenFridge.setFSval(-5f);
  612. System.out.println("Training:");//TODO
  613. for(int i=0; i<24; i++)
  614. sim.getSimulationManager().simulateTimeIntervall(i*hour, hour);
  615. long new_time = System.currentTimeMillis();
  616. long elapsed_time = new_time-currentTime;
  617. System.out.println("Training data generation: "+elapsed_time+"ms");
  618. currentTime = new_time;
  619. collectorKNN.setMode(true);
  620. //collectorEM.setMode(true);
  621. //collectorHC.setMode(true);
  622. new_time = System.currentTimeMillis();
  623. elapsed_time = new_time-currentTime;
  624. System.out.println("Training of algorithm: "+elapsed_time+"ms");
  625. currentTime = new_time;
  626. /*
  627. * Simulate/Test 1 hour without anomalies
  628. */
  629. System.out.println("Test w/0 anomalies:");//TODO
  630. for(int i=24; i<25; i++)
  631. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  632. new_time = System.currentTimeMillis();
  633. elapsed_time = new_time-currentTime;
  634. System.out.println("Training data generation: "+elapsed_time+"ms");
  635. currentTime = new_time;
  636. System.out.println("DDoS:");
  637. /**
  638. * 1 hour DDoS
  639. */
  640. Connection ddos = anomalyController.openDDosCreationMenu(broker, new LinkedList<SmartDevice>(zigbee.getDevices()));
  641. for(int i=25; i<26; i++)
  642. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  643. networkController.deleteConnection(ddos);
  644. new_time = System.currentTimeMillis();
  645. elapsed_time = new_time-currentTime;
  646. System.out.println("DDoS generation & classification: "+elapsed_time+"ms");
  647. currentTime = new_time;
  648. System.out.println("DoS:");
  649. /**
  650. * 1 hour DoS
  651. */
  652. Connection dos = anomalyController.runDosAttack(kitchenLight, kitchenFridge);
  653. for(int i=26; i<27; i++)
  654. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  655. networkController.deleteConnection(dos);
  656. new_time = System.currentTimeMillis();
  657. elapsed_time = new_time-currentTime;
  658. System.out.println("DoS generation & classification: "+elapsed_time+"ms");
  659. currentTime = new_time;
  660. /**
  661. * Value Anomaly 1h
  662. */
  663. System.out.println("Value Anomalies:");
  664. float min = kitchenFridge.getFSmin();
  665. float max = kitchenFridge.getFSmax();
  666. float val = kitchenFridge.getFSval();
  667. kitchenFridge.setFSmin(18);
  668. kitchenFridge.setFSval(18.5f);
  669. kitchenFridge.setFSmax(24);
  670. kitchenFridge.setLabel((short)-1);//-1 Value anomaly
  671. for(int i=27; i<28; i++)
  672. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  673. kitchenFridge.setFSmin(min);
  674. kitchenFridge.setFSmax(max);
  675. kitchenFridge.setFSval(val);
  676. kitchenFridge.setLabel((short)0);
  677. new_time = System.currentTimeMillis();
  678. elapsed_time = new_time-currentTime;
  679. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  680. currentTime = new_time;
  681. System.out.println("Testing completed");
  682. } catch(Exception e) {
  683. System.out.println("WARNING: Testing failed: ");
  684. e.printStackTrace();
  685. }
  686. }
  687. /**
  688. * Creates the paper toy example, also simulates it, if run = true
  689. * @param run
  690. */
  691. @SuppressWarnings("unchecked")
  692. public void createSWCExample(boolean run){
  693. /**
  694. * Reset network, etc.
  695. */
  696. long hour = 60 * 60 * 1000;
  697. long endTime = 24 * hour;
  698. long noAnomEnd = endTime + 1 * hour;
  699. long lightEnd = noAnomEnd + 1 * hour;
  700. long tempEnd = lightEnd + 1 * hour;
  701. long stepLength=hour;
  702. long currentSimTime=0;
  703. SimulationController sim = controller.getSimulationController();
  704. if(run) {
  705. controller.getSimulationController().resetSimulation();
  706. controller.getNetworkController().deleteNetworkModel();
  707. /*
  708. * Simulate 24 hours
  709. */
  710. sim.setStartTime(0);
  711. sim.resetSimulation();
  712. sim.setStepDuration(hour);
  713. sim.setPrintPackets(false);
  714. sim.setEndTime(endTime);
  715. }
  716. /*
  717. * Main networking devices
  718. */
  719. SmartDevice smartHub = networkController.createSmartDevice("SmartHub", 400, 400, 50);
  720. /*
  721. * Links/Networks
  722. */
  723. Link zigbee = new PrecisionLink("ZigBee");
  724. networkController.addLink(zigbee);
  725. /*
  726. * Connect Devices to Links
  727. */
  728. networkController.addLinkToDevice(zigbee, smartHub);
  729. /*
  730. * Create MQTT Connection
  731. */
  732. Connection mqtt = new ConnectionPrecision();
  733. mqtt.setName("HomeAutomation (MQTT)");
  734. networkController.addConnectionToLink(mqtt, zigbee);
  735. mqtt.setProtocol(new MQTT_protocol());
  736. Port pBroker = new Port(smartHub, (short)1883);
  737. pBroker.setStatus(Port.OPEN);
  738. pBroker.setResponseTime((short)2);
  739. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  740. networkController.addConnection(mqtt);
  741. mqtt.setLabel((short) 0);
  742. /**
  743. * Room
  744. */
  745. String roomName = "Bedroom";
  746. RoomStatus room = new RoomStatus();
  747. room.setTemperature(19f);
  748. /*
  749. * Add some MQTT Devices
  750. */
  751. /**
  752. * Room Thermostat
  753. */
  754. SmartTemperatureSensor heatSensor = new SmartTemperatureSensor(roomName + " Thermostat", room);
  755. heatSensor.setFSinfoName("home/" + roomName + "/sensorTemp");
  756. networkController.addLinkToDevice(zigbee, heatSensor);
  757. networkController.moveSmartDevice(heatSensor, 300, 300, 50);
  758. heatSensor.setFSmin(19.0f);
  759. heatSensor.setFSmax(21.0f);
  760. heatSensor.setFSval(19.0f);
  761. networkController.addSmartDevice(heatSensor);
  762. Port pFloatSensor = new Port(heatSensor, (short)1883, 15000);
  763. pFloatSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  764. pFloatSensor.setStatus(Port.SENDING);
  765. pFloatSensor.setLastTrigger(-357L);
  766. networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
  767. heatSensor.setLabel((short) 0);
  768. /*
  769. * Add Heater (every second update)
  770. */
  771. SmartTemperatureProducer heaterDevice = new SmartTemperatureProducer(roomName + " Heater", room, 500, heatSensor);
  772. heaterDevice.setFSinfoName("home/" + roomName + "/heaterTemp");
  773. networkController.addLinkToDevice(zigbee, heaterDevice);
  774. networkController.moveSmartDevice(heaterDevice, 300, 500, 50);
  775. heaterDevice.setFSmin(19.0f);
  776. heaterDevice.setFSmax(21.0f);
  777. heaterDevice.setFSval(19.0f);
  778. networkController.addSmartDevice(heaterDevice);
  779. Port pHeaterDevice = new Port(heaterDevice, (short)1883, 15000);
  780. pHeaterDevice.setStatus(Port.SENDING);
  781. pHeaterDevice.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  782. pHeaterDevice.setLastTrigger(-1231L);
  783. networkController.addDeviceToConnectionAndProtocol(pHeaterDevice, mqtt,1);
  784. heaterDevice.setLabel((short) 0);
  785. /*
  786. * Add light Sensor
  787. */
  788. SmartLightSensor lightSensor = new SmartLightSensor(roomName + " LightSensor", room, 1000);
  789. lightSensor.setBSinfoName("home/" + roomName + "/light");
  790. networkController.addLinkToDevice(zigbee, lightSensor);
  791. networkController.moveSmartDevice(lightSensor, 500, 300, 50);
  792. networkController.addSmartDevice(lightSensor);
  793. Port pLightSensor = new Port(lightSensor, (short)1883, 15000);
  794. pLightSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  795. pLightSensor.setStatus(Port.SENDING);
  796. pLightSensor.setLastTrigger(-1337L);
  797. networkController.addDeviceToConnectionAndProtocol(pLightSensor, mqtt,1);
  798. lightSensor.setLabel((short) 0);
  799. /*
  800. * Add a light
  801. */
  802. SmartLight smartLight = new SmartLight(roomName + " Light", room, lightSensor);
  803. smartLight.setBSinfoName("home/" + roomName + "/light");
  804. networkController.addLinkToDevice(zigbee, smartLight);
  805. networkController.moveSmartDevice(smartLight, 500, 500, 50);
  806. networkController.addSmartDevice(smartLight);
  807. Port pSmartLight = new Port(smartLight, (short)1883, 15000);
  808. pSmartLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  809. pSmartLight.setStatus(Port.SENDING);
  810. pSmartLight.setLastTrigger(-1207L);
  811. networkController.addDeviceToConnectionAndProtocol(pSmartLight, mqtt,1);
  812. smartLight.setLabel((short) 0);
  813. /*
  814. * Update visualization
  815. */
  816. controller.notifyObservers();
  817. /**
  818. * Run only if run == true
  819. */
  820. if(!run)return;
  821. try {
  822. System.out.println("Check 1");//TODO
  823. /**
  824. * Instances of the packet Sniffers
  825. */
  826. SWCKMeansClusteringWithLabels snifferKNNwithLabels = new SWCKMeansClusteringWithLabels();
  827. SWCKMeansClustering snifferKNNnoLabels = new SWCKMeansClustering();
  828. PacketCollector collectorKNNwithLabels = new PacketCollector(snifferKNNwithLabels);
  829. PacketCollector collectorKNNnoLabels = new PacketCollector(snifferKNNnoLabels);
  830. //PacketCollector collectorEM = new PacketCollector(snifferEM);
  831. //PacketCollector collectorHC = new PacketCollector(snifferHC);
  832. /*
  833. * Capture both links on all collectors
  834. */
  835. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  836. //captureController.addLinkToCollector(collectorEM, zigbee);
  837. //captureController.addLinkToCollector(collectorKNN, zigbee);
  838. captureController.addDeviceToCollector(collectorKNNwithLabels, smartLight);
  839. captureController.addDeviceToCollector(collectorKNNwithLabels, heaterDevice);
  840. captureController.addPacketCollector(collectorKNNwithLabels);
  841. captureController.addDeviceToCollector(collectorKNNnoLabels, smartLight);
  842. captureController.addDeviceToCollector(collectorKNNnoLabels, heaterDevice);
  843. captureController.addPacketCollector(collectorKNNnoLabels);
  844. //captureController.addPacketCollector(collectorEM);
  845. //captureController.addPacketCollector(collectorHC);
  846. System.out.println("Check 3: created Controller");//TODO
  847. long currentTime = System.currentTimeMillis();
  848. /**
  849. * Training events
  850. */
  851. System.out.println("Create training events");
  852. /**
  853. * Light Events (random switch every 30min+-15min
  854. */
  855. NormalDistribution lightDist = new NormalDistribution(hour/10, hour/20);
  856. long eventTime = 0;
  857. boolean on = true;
  858. while(eventTime < noAnomEnd) {
  859. /**
  860. * At least 1 second between events
  861. */
  862. final boolean newVal = on;
  863. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  864. @Override
  865. public void simulateEvent(long time) {
  866. smartLight.setTrueStatus(newVal);
  867. smartLight.setBSval(newVal);
  868. }
  869. });
  870. on = !on;
  871. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  872. }
  873. /**
  874. * Heater Events (random switch every 2h +- 30min
  875. */
  876. NormalDistribution tempDist = new NormalDistribution(2*hour, hour/2);
  877. eventTime = 0;
  878. boolean low = true;
  879. while(eventTime < lightEnd) {
  880. /**
  881. * At least 1 second between events
  882. */
  883. final float newTemp;
  884. if(low)
  885. newTemp = heaterDevice.getFSmax();
  886. else
  887. newTemp = heaterDevice.getFSmin();
  888. low = !low;
  889. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  890. @Override
  891. public void simulateEvent(long time) {
  892. heaterDevice.setFSval(newTemp);
  893. heaterDevice.setTrueTemperature(newTemp);
  894. }
  895. });
  896. eventTime+=Math.max(1000, Math.round(tempDist.sample()));
  897. }
  898. System.out.println("Training:");//TODO
  899. while(currentSimTime<endTime) {
  900. sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
  901. currentSimTime+=stepLength;
  902. }
  903. long new_time = System.currentTimeMillis();
  904. long elapsed_time = new_time-currentTime;
  905. System.out.println("Training data generation took: "+elapsed_time+"ms");
  906. currentTime = new_time;
  907. /*
  908. * Start Classifying
  909. */
  910. collectorKNNwithLabels.setMode(true);
  911. collectorKNNnoLabels.setMode(true);
  912. //collectorEM.setMode(true);
  913. //collectorHC.setMode(true);
  914. new_time = System.currentTimeMillis();
  915. elapsed_time = new_time-currentTime;
  916. System.out.println("Training of algorithm took: "+elapsed_time+"ms");
  917. currentTime = new_time;
  918. /*
  919. * Simulate/Test 2 hour without anomalies
  920. */
  921. snifferKNNwithLabels.setCurrentScenario("NoAnomalies");
  922. snifferKNNnoLabels.setCurrentScenario("NoAnomalies");
  923. System.out.println("Test w/0 anomalies:");//TODO
  924. sim.getSimulationManager().simulateTimeIntervall(currentSimTime, noAnomEnd-currentSimTime);
  925. currentSimTime=noAnomEnd;
  926. new_time = System.currentTimeMillis();
  927. elapsed_time = new_time-currentTime;
  928. System.out.println("Test witout anomalies took: "+elapsed_time+"ms");
  929. currentTime = new_time;
  930. /**
  931. * Light Anomaly 1h
  932. * Light != Sensor
  933. */
  934. snifferKNNwithLabels.setCurrentScenario("LightAnomalies");
  935. snifferKNNnoLabels.setCurrentScenario("LightAnomalies");
  936. System.out.println("Light Anomaly:");
  937. eventTime = noAnomEnd;
  938. on = true;
  939. smartLight.setTrueStatus(false);
  940. smartLight.setBSval(true);
  941. while(eventTime < lightEnd) {
  942. /**
  943. * At least 1 second between events
  944. */
  945. final boolean newVal = on;
  946. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  947. @Override
  948. public void simulateEvent(long time) {
  949. smartLight.setTrueStatus(newVal);
  950. smartLight.setBSval(!newVal);
  951. }
  952. });
  953. on = !on;
  954. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  955. }
  956. smartLight.setLabel((short)-1);
  957. sim.getSimulationManager().simulateTimeIntervall(currentSimTime, lightEnd-currentSimTime);
  958. currentSimTime=lightEnd;
  959. smartLight.setBSval(smartLight.isTrueStatus());
  960. smartLight.setLabel((short)0);
  961. new_time = System.currentTimeMillis();
  962. elapsed_time = new_time-currentTime;
  963. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  964. currentTime = new_time;
  965. /**
  966. * Schedule new light events
  967. */
  968. eventTime = lightEnd;
  969. on = true;
  970. while(eventTime < tempEnd) {
  971. /**
  972. * At least 1 second between events
  973. */
  974. final boolean newVal = on;
  975. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  976. @Override
  977. public void simulateEvent(long time) {
  978. smartLight.setTrueStatus(newVal);
  979. smartLight.setBSval(newVal);
  980. }
  981. });
  982. on = !on;
  983. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  984. }
  985. /**
  986. * Temperature Anomaly 2h
  987. */
  988. snifferKNNwithLabels.setCurrentScenario("TemperatureAnomalies");
  989. snifferKNNnoLabels.setCurrentScenario("TemperatureAnomalies");
  990. System.out.println("Temperature Anomaly:");
  991. heaterDevice.setFSval(21f);
  992. heaterDevice.setTrueTemperature(34f);
  993. heaterDevice.setLabel((short)-1);//-1 Value anomaly
  994. sim.getSimulationManager().simulateTimeIntervall(currentSimTime, tempEnd-currentSimTime);
  995. currentSimTime=tempEnd;
  996. heaterDevice.setFSval(21f);
  997. heaterDevice.setTrueTemperature(21f);
  998. heaterDevice.setLabel((short)0);
  999. new_time = System.currentTimeMillis();
  1000. elapsed_time = new_time-currentTime;
  1001. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  1002. currentTime = new_time;
  1003. System.out.println("Testing completed");
  1004. } catch(Exception e) {
  1005. System.out.println("WARNING: Testing failed: ");
  1006. e.printStackTrace();
  1007. }
  1008. }
  1009. }