MenuBarNetworkExamples.java 39 KB

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