MenuBarNetworkExamples.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. private void initializePerformanceTest(){
  117. SmartDevice tic = new SmartDevice("Tic");
  118. tic.setX(100);
  119. tic.setY(100);
  120. SmartDevice toc = new SmartDevice("Toc");
  121. toc.setX(100);
  122. toc.setY(250);
  123. networkController.addSmartDevice(tic);
  124. networkController.addSmartDevice(toc);
  125. Link l = new PrecisionLink("Channel");
  126. networkController.addLink(l);
  127. networkController.addLinkToDevice(l, tic);
  128. networkController.addLinkToDevice(l, toc);
  129. Port ticP = new Port(tic, (short)2, 200, (short)0, 0, (short)0);
  130. Port tocP = new Port(toc, (short)3, 200, (short)0, -100, (short)0);
  131. tic.addPort(ticP);
  132. toc.addPort(tocP);
  133. Connection con = new ConnectionPrecision(l, new SimpleProtocol());
  134. networkController.addConnection(con);
  135. networkController.addConnectionToLink(con, l);
  136. networkController.addDeviceToConnectionAndProtocol(ticP, con, 0);
  137. networkController.addDeviceToConnectionAndProtocol(tocP, con, 1);
  138. simulationController.setStepDuration(100000000);
  139. simulationController.setEndTime(1000000000);
  140. controller.notifyObservers();
  141. }
  142. /**
  143. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  144. */
  145. private void initializeUserStudy(){
  146. /**
  147. * Preset one SmartDevice
  148. */
  149. SmartDevice A = new SmartDevice("Homestation");
  150. A.setX(200);
  151. A.setY(200);
  152. networkController.addSmartDevice(A);
  153. /**
  154. * Packagecollector, which is registered at the Device
  155. */
  156. PacketCollector collector = new PacketCollector(new CountingMetric());
  157. simulationController.getPacketCaptureController().addPacketCollector(collector);
  158. simulationController.getPacketCaptureController().addDeviceToCollector(collector, A);
  159. simulationController.setPrintPackets(true);
  160. controller.notifyObservers();
  161. }
  162. /**
  163. * Test package collectors
  164. */
  165. private void testPackageCollection() {
  166. if(networkController.getSmartDevices().size()<3)
  167. return;
  168. simulationController.addAlgorithm(new Manipulation_RandomMove(), controller);
  169. PacketCollector collector = new PacketCollector();
  170. simulationController.getSimulationManager().getPacketCollectionManager().addPacketCollector(collector);
  171. System.out.println("Collector 0-500 - nothing collected:");
  172. simulationController.getSimulationManager().simulateTimeIntervall(0, 500);
  173. HashMap<Link, LinkedList<Packet>> map = collector.getPackets();
  174. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  175. System.out.println("Link: "+e.getKey().getName());
  176. for(Packet p : e.getValue())
  177. System.out.println(p.getTextualRepresentation());
  178. }
  179. System.out.println("");
  180. System.out.println("");
  181. Iterator<SmartDevice> it = networkController.getSmartDevices().iterator();
  182. it.next();
  183. SmartDevice d = it.next();
  184. collector.addDevice(d);
  185. SmartDevice f = it.next();
  186. collector.addDevice(f);
  187. System.out.println("Collector 500-1000 - "+d.getName()+" & "+f.getName()+" collected:");
  188. simulationController.getSimulationManager().simulateTimeIntervall(500, 500);
  189. map = collector.getPackets();
  190. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  191. System.out.println("Link: "+e.getKey().getName());
  192. for(Packet p : e.getValue())
  193. System.out.println(p.getTextualRepresentation());
  194. }
  195. System.out.println("");
  196. System.out.println("");
  197. Link l = networkController.getLinks().iterator().next();
  198. collector.addLink(l);
  199. System.out.println("Collector 2000-3000 - "+l+" collected:");
  200. simulationController.getSimulationManager().simulateTimeIntervall(1000, 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. simulationController.resetSimulation();
  208. controller.notifyObservers();
  209. }
  210. /**
  211. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  212. */
  213. private void initializeTest(){
  214. SmartDevice A = null, B = null, C = null;
  215. for(int i = 0; i<5; i++){
  216. A = new SmartDevice("SmartTV"+i);
  217. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  218. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  219. B = new SmartDevice("SmartDoor"+i);
  220. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  221. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  222. C = new SmartDevice("SmartLight"+i);
  223. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  224. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  225. networkController.addSmartDevice(A);
  226. networkController.addSmartDevice(B);
  227. networkController.addSmartDevice(C);
  228. }
  229. Link link = new SimpleLink("SimpleWifi");
  230. link.addDevice(A);
  231. link.addDevice(B);
  232. link.addDevice(C);
  233. A.addLink(link);
  234. B.addLink(link);
  235. C.addLink(link);
  236. Port a = new Port(A, (short) 1, 68);
  237. a.setLastTrigger(0);
  238. a.setStatus(Port.SENDING);
  239. A.addPort(a);
  240. Port b = new Port(B, (short) 2, 102);
  241. b.setStatus(Port.SENDING);
  242. B.addPort(b);
  243. Connection s = new ConnectionPerformance(link, new SimpleProtocol(a, b));
  244. s.setPacketLossProbability(0.01);//1% Packet loss probability
  245. a.setConnection(s);
  246. b.setConnection(s);
  247. s.addSmartDevice(a);
  248. s.addSmartDevice(b);
  249. link.addConnection(s);
  250. networkController.addLink(link);
  251. networkController.addConnection(s);
  252. controller.notifyObservers();
  253. }
  254. /**
  255. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  256. */
  257. private void initializeMQTTTest(){
  258. SimpleLink link = new SimpleLink("WIFI");
  259. link.setFixedDelay(5);
  260. SmartDevice broker = new SmartDevice("MQTT-Broker");
  261. broker.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  262. broker.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  263. networkController.addSmartDevice(broker);
  264. Port brokerPort = new Port(broker, (short) 0, 987);
  265. brokerPort.setLastTrigger(0);
  266. brokerPort.setStatus(Port.OPEN);
  267. broker.addPort(brokerPort);
  268. link.addDevice(broker);
  269. broker.addLink(link);
  270. Protocol protocol = new MQTT_protocol(brokerPort);
  271. Connection con = new ConnectionPrecision(link, protocol);
  272. con.setPacketLossProbability(0.01);//1% Packet loss probability
  273. con.addSmartDevice(brokerPort);
  274. brokerPort.setConnection(con);
  275. con.setStatus(Connection.ACTIVE);
  276. networkController.addLink(link);
  277. link.addConnection(con);
  278. networkController.addConnection(con);
  279. SmartDevice A = null, A1 = null, A2 = null, B = null, C = null;
  280. Port aP,a1P,a2P,bP,cP;
  281. for(int i = 0; i<3; i++){
  282. FloatSensorDevice aS = new FloatSensorDevice("SmartThermostat"+i+"(Pub)");
  283. aS.setFSinfoName("room"+i+"/temperature");
  284. A=aS;
  285. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  286. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  287. link.addDevice(A);
  288. A.addLink(link);
  289. aP = new Port(A,((short) (3*i+1)),100+(int)(Math.random()*900));
  290. aP.setLastTrigger(0);
  291. aP.setJitter((short) (Math.random()*50));
  292. aP.setStatus(Port.SENDING);
  293. aP.setConnection(con);
  294. protocol.addDeviceOfRole(aP, 2);
  295. con.addSmartDevice(aP);
  296. A.addPort(aP);
  297. networkController.addSmartDevice(A);
  298. FloatCollectorDevice aS1 = new FloatCollectorDevice("SmartAirCondition"+i+"(Sub)");
  299. aS1.setFCinfoName("room"+i+"/temperature");
  300. A1=aS1;
  301. A1.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  302. A1.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  303. link.addDevice(A1);
  304. A1.addLink(link);
  305. a1P = new Port(A1,((short) (3*i+1)),100+(int)(Math.random()*900));
  306. a1P.setLastTrigger(0);
  307. a1P.setJitter((short) (Math.random()*50));
  308. a1P.setStatus(Port.SENDING);
  309. a1P.setConnection(con);
  310. protocol.addDeviceOfRole(a1P, 3);
  311. con.addSmartDevice(a1P);
  312. A1.addPort(a1P);
  313. networkController.addSmartDevice(A1);
  314. FloatCollectorDevice aS2 = new FloatCollectorDevice("SmartHeater"+i+"(Sub)");
  315. aS2.setFCinfoName("room"+i+"/temperature");
  316. A2=aS2;
  317. A2.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  318. A2.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  319. link.addDevice(A2);
  320. A2.addLink(link);
  321. a2P = new Port(A2,((short) (3*i+1)),100+(int)(Math.random()*900));
  322. a2P.setLastTrigger(0);
  323. a2P.setJitter((short) (Math.random()*50));
  324. a2P.setStatus(Port.SENDING);
  325. a2P.setConnection(con);
  326. protocol.addDeviceOfRole(a2P, 3);
  327. con.addSmartDevice(a2P);
  328. A2.addPort(a2P);
  329. networkController.addSmartDevice(A2);
  330. B = new BoolSensorDevice("SmartDoor"+i+"(Pub)");
  331. ((BoolSensorDevice)B).setBSinfoName("room"+i+"/DoorOpen");
  332. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  333. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  334. link.addDevice(B);
  335. B.addLink(link);
  336. bP = new Port(B,((short) (3*i+2)),10+(int)(Math.random()*190));
  337. bP.setLastTrigger(0);
  338. bP.setJitter((short) (Math.random()*50));
  339. bP.setStatus(Port.SENDING);
  340. bP.setConnection(con);
  341. protocol.addDeviceOfRole(bP, 2);
  342. con.addSmartDevice(bP);
  343. B.addPort(bP);
  344. networkController.addSmartDevice(B);
  345. C = new BoolCollectorDevice("SmartDoorStatusLight"+i+"(Sub)");
  346. ((BoolCollectorDevice)C).setBCinfoName("room"+i+"/DoorOpen");
  347. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  348. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  349. link.addDevice(C);
  350. C.addLink(link);
  351. cP = new Port(C,((short) (3*i+1)), 50+(int)(Math.random()*450));
  352. cP.setLastTrigger(0);
  353. cP.setJitter((short) (Math.random()*50));
  354. cP.setStatus(Port.SENDING);
  355. cP.setConnection(con);
  356. protocol.addDeviceOfRole(cP, 3);
  357. con.addSmartDevice(cP);
  358. C.addPort(cP);
  359. networkController.addSmartDevice(C);
  360. controller.notifyObservers();
  361. }
  362. }
  363. /**
  364. * Creates the paper toy example, also simulates it, if run = true
  365. * @param run
  366. */
  367. @SuppressWarnings("unchecked")
  368. public void createPaperExample(boolean run){
  369. /**
  370. * Reset network, etc.
  371. */
  372. if(run) {
  373. controller.getSimulationController().resetSimulation();
  374. controller.getNetworkController().deleteNetworkModel();
  375. }
  376. /*
  377. * Main networking devices
  378. */
  379. SmartDevice router = networkController.createSmartDevice("Wifi-Router", 500, 100, 50);
  380. SmartDevice zigBeeRouter = networkController.createSmartDevice("ZigBee-Router", 500, 300, 50);
  381. SmartDevice broker = networkController.createSmartDevice("SmartHub", 500, 500, 50);
  382. /*
  383. * Links/Networks
  384. */
  385. Link wifi = new PrecisionLink("Wifi");
  386. networkController.addLink(wifi);
  387. Link zigbee = new PrecisionLink("ZigBee");
  388. networkController.addLink(zigbee);
  389. /*
  390. * Connect Devices to Links
  391. */
  392. networkController.addLinkToDevice(wifi, router);
  393. networkController.addLinkToDevice(wifi, zigBeeRouter);
  394. networkController.addLinkToDevice(zigbee, zigBeeRouter);
  395. networkController.addLinkToDevice(zigbee, broker);
  396. /*
  397. * Internet Access Connection
  398. */
  399. Connection inetAcces = new ConnectionPrecision();
  400. inetAcces.setName("Cloud Access");
  401. networkController.addConnectionToLink(inetAcces, wifi);
  402. inetAcces.setProtocol(new SimpleProtocol());
  403. Port pRouter = new Port(router, (short)80, 500L);
  404. pRouter.setTriggerHandler(new NormalDistributionHandler(500, 100));
  405. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  406. Port pZigBee = new Port(zigBeeRouter, (short)80, 1000L);
  407. pZigBee.setTriggerHandler(new NormalDistributionHandler(1000, 300));
  408. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  409. networkController.addDeviceToConnectionAndProtocol(pZigBee, inetAcces, 1);
  410. networkController.addConnection(inetAcces);
  411. /*
  412. * ZigbeeRouter -> Broker ?
  413. */
  414. Connection homeAutomationInternetAccess = new ConnectionPrecision();
  415. homeAutomationInternetAccess.setName("Home Automation Webinterface");
  416. networkController.addConnectionToLink(homeAutomationInternetAccess, zigbee);
  417. homeAutomationInternetAccess.setProtocol(new SimpleProtocol());
  418. Port pBrokerWebInterface = new Port(broker, (short)80);
  419. pBrokerWebInterface.setTriggerHandler(new NormalDistributionHandler(2000, 500));
  420. pBrokerWebInterface.setStatus(Port.SENDING);
  421. pBrokerWebInterface.setResponseTime((short)2);
  422. pBrokerWebInterface.setLastTrigger(-284L);
  423. networkController.addDeviceToConnectionAndProtocol(pBrokerWebInterface, homeAutomationInternetAccess, 0);
  424. networkController.addConnection(homeAutomationInternetAccess);
  425. Port pRouterWebInterface = new Port(zigBeeRouter, (short)80);
  426. pRouterWebInterface.setTriggerHandler(new NormalDistributionHandler(5000, 3000));
  427. pRouterWebInterface.setStatus(Port.SENDING);
  428. pRouterWebInterface.setResponseTime((short)2);
  429. pRouterWebInterface.setLastTrigger(-142L);
  430. networkController.addDeviceToConnectionAndProtocol(pRouterWebInterface, homeAutomationInternetAccess, 1);
  431. networkController.addConnection(homeAutomationInternetAccess);
  432. /*
  433. * Create MQTT Connection
  434. */
  435. Connection mqtt = new ConnectionPrecision();
  436. mqtt.setName("Automation (MQTT)");
  437. networkController.addConnectionToLink(mqtt, zigbee);
  438. mqtt.setProtocol(new MQTT_protocol());
  439. Port pBroker = new Port(broker, (short)1883);
  440. pBroker.setStatus(Port.OPEN);
  441. pBroker.setResponseTime((short)2);
  442. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  443. networkController.addConnection(mqtt);
  444. /*
  445. * Add some MQTT Devices
  446. */
  447. /**
  448. * Kitchen Thermostat
  449. */
  450. FloatSensorDevice floatSensor = new FloatSensorDevice("Kitchen Thermostat");
  451. floatSensor.setFSinfoName("home/kitchen/temperature");
  452. networkController.addLinkToDevice(zigbee, floatSensor);
  453. networkController.moveSmartDevice(floatSensor, 300, 500, 50);
  454. floatSensor.setFSmin(15.0f);
  455. floatSensor.setFSmax(32.0f);
  456. networkController.addSmartDevice(floatSensor);
  457. Port pFloatSensor = new Port(floatSensor, (short)1883, 15000);
  458. pFloatSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  459. pFloatSensor.setStatus(Port.SENDING);
  460. pFloatSensor.setLastTrigger(-357L);
  461. networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
  462. /*
  463. * Add Fridge
  464. */
  465. FloatSensorDevice kitchenFridge = new FloatSensorDevice("Smart Fridge");
  466. kitchenFridge.setFSinfoName("home/kitchen/fridgeTemp");
  467. networkController.addLinkToDevice(zigbee, kitchenFridge);
  468. networkController.moveSmartDevice(kitchenFridge, 100, 600, 50);
  469. kitchenFridge.setFSmin(-6.0f);
  470. kitchenFridge.setFSmax(-4.0f);
  471. networkController.addSmartDevice(kitchenFridge);
  472. Port pKitchenFridge = new Port(kitchenFridge, (short)1883, 15000);
  473. pKitchenFridge.setStatus(Port.SENDING);
  474. pKitchenFridge.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  475. pKitchenFridge.setLastTrigger(-1231L);
  476. networkController.addDeviceToConnectionAndProtocol(pKitchenFridge, mqtt,1);
  477. /*
  478. * Add some kitchen lights
  479. */
  480. BoolSensorDevice kitchenLight = new BoolSensorDevice("Kitchen Light");
  481. kitchenLight.setBSinfoName("home/kitchen/light");
  482. networkController.addLinkToDevice(zigbee, kitchenLight);
  483. networkController.moveSmartDevice(kitchenLight, 250, 400, 50);
  484. networkController.addSmartDevice(kitchenLight);
  485. Port pKitchenLight = new Port(kitchenLight, (short)1883, 15000);
  486. pKitchenLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  487. pKitchenLight.setStatus(Port.SENDING);
  488. pKitchenLight.setLastTrigger(-1207L);
  489. networkController.addDeviceToConnectionAndProtocol(pKitchenLight, mqtt,1);
  490. //TODO Further devices & Connections
  491. /*
  492. * Bedroom
  493. */
  494. BoolSensorDevice sleepingRoomLight = new BoolSensorDevice("Bedroom Light");
  495. sleepingRoomLight.setBSinfoName("home/bedroom/light");
  496. networkController.addLinkToDevice(zigbee, sleepingRoomLight);
  497. networkController.moveSmartDevice(sleepingRoomLight, 750, 400, 50);
  498. networkController.addSmartDevice(sleepingRoomLight);
  499. Port pBedroomLight = new Port(sleepingRoomLight, (short)1883, 15000);
  500. pBedroomLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  501. pBedroomLight.setStatus(Port.SENDING);
  502. pBedroomLight.setLastTrigger(-1337L);
  503. networkController.addDeviceToConnectionAndProtocol(pBedroomLight, mqtt,1);
  504. /*
  505. * Bedroom Thermostat
  506. */
  507. FloatSensorDevice bedroomThermostat = new FloatSensorDevice("Bedroom Thermostat");
  508. bedroomThermostat.setFSinfoName("home/bedroom/temperature");
  509. networkController.addLinkToDevice(zigbee, bedroomThermostat);
  510. networkController.moveSmartDevice(bedroomThermostat, 700, 500, 50);
  511. bedroomThermostat.setFSmin(15.0f);
  512. bedroomThermostat.setFSmax(32.0f);
  513. networkController.addSmartDevice(bedroomThermostat);
  514. Port pBedroomThermostat = new Port(bedroomThermostat, (short)1883, 15000);
  515. pBedroomThermostat.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  516. pBedroomThermostat.setStatus(Port.SENDING);
  517. pBedroomThermostat.setLastTrigger(-820L);
  518. networkController.addDeviceToConnectionAndProtocol(pBedroomThermostat, mqtt,1);
  519. /*
  520. * Bedroom Info Screen
  521. */
  522. FloatCollectorDevice bedroomInfoScreen = new FloatCollectorDevice("Information Panel");
  523. bedroomInfoScreen.setFCinfoName("home/kitchen/fridgeTemp");
  524. networkController.addLinkToDevice(zigbee, bedroomInfoScreen);
  525. networkController.moveSmartDevice(bedroomInfoScreen, 900, 600, 50);
  526. networkController.addSmartDevice(bedroomInfoScreen);
  527. Port pBedroomInfo = new Port(bedroomInfoScreen, (short)1883, 15000);
  528. pBedroomInfo.setStatus(Port.SENDING);
  529. pBedroomInfo.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  530. pBedroomInfo.setLastTrigger(-666L);
  531. networkController.addDeviceToConnectionAndProtocol(pBedroomInfo, mqtt,1);
  532. /*
  533. * Update visualization
  534. */
  535. controller.notifyObservers();
  536. /**
  537. * Run only if run == true
  538. */
  539. if(!run)return;
  540. try {
  541. System.out.println("Check 1");//TODO
  542. /**
  543. * Instances of the packet Sniffers
  544. */
  545. PacketSniffer snifferEM = null, snifferKNN = null, snifferHC = null;
  546. /*
  547. * Import Example PacketSniffer algorithms
  548. */
  549. Class<? extends PacketSniffer> em = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/EMClustering.java"));
  550. snifferEM = em.newInstance();
  551. Class<? extends PacketSniffer> knn = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/KMeansClustering.java"));
  552. snifferKNN = knn.newInstance();
  553. Class<? extends PacketSniffer> hc = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/HierarchicalClustering.java"));
  554. snifferHC = hc.newInstance();
  555. System.out.println("Check 2: Imported");//TODO
  556. /*
  557. * Create collectors
  558. */
  559. PacketCollector collectorKNN = new PacketCollector(snifferKNN);
  560. //PacketCollector collectorEM = new PacketCollector(snifferEM);
  561. //PacketCollector collectorHC = new PacketCollector(snifferHC);
  562. /*
  563. * Capture both links on all collectors
  564. */
  565. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  566. //captureController.addLinkToCollector(collectorEM, zigbee);
  567. //captureController.addLinkToCollector(collectorEM, wifi);
  568. captureController.addLinkToCollector(collectorKNN, zigbee);
  569. captureController.addLinkToCollector(collectorKNN, wifi);
  570. //captureController.addLinkToCollector(collectorHC, zigbee);
  571. //captureController.addLinkToCollector(collectorHC, wifi);
  572. captureController.addPacketCollector(collectorKNN);
  573. //captureController.addPacketCollector(collectorEM);
  574. //captureController.addPacketCollector(collectorHC);
  575. System.out.println("Check 3: created Controller");//TODO
  576. long currentTime = System.currentTimeMillis();
  577. long hour = 60 * 60 * 1000;
  578. /*
  579. * Simulate 24 hours
  580. */
  581. SimulationController sim = controller.getSimulationController();
  582. sim.setStartTime(0);
  583. sim.resetSimulation();
  584. sim.setStepDuration(hour);
  585. sim.setPrintPackets(false);
  586. sim.setEndTime(24*hour);
  587. kitchenFridge.setFSmin(-6f);
  588. kitchenFridge.setFSmax(-4f);
  589. kitchenFridge.setFSval(-5f);
  590. System.out.println("Training:");//TODO
  591. for(int i=0; i<24; i++)
  592. sim.getSimulationManager().simulateTimeIntervall(i*hour, hour);
  593. long new_time = System.currentTimeMillis();
  594. long elapsed_time = new_time-currentTime;
  595. System.out.println("Training data generation: "+elapsed_time+"ms");
  596. currentTime = new_time;
  597. collectorKNN.setMode(true);
  598. //collectorEM.setMode(true);
  599. //collectorHC.setMode(true);
  600. new_time = System.currentTimeMillis();
  601. elapsed_time = new_time-currentTime;
  602. System.out.println("Training of algorithm: "+elapsed_time+"ms");
  603. currentTime = new_time;
  604. /*
  605. * Simulate/Test 1 hour without anomalies
  606. */
  607. System.out.println("Test w/0 anomalies:");//TODO
  608. for(int i=24; i<25; i++)
  609. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  610. new_time = System.currentTimeMillis();
  611. elapsed_time = new_time-currentTime;
  612. System.out.println("Training data generation: "+elapsed_time+"ms");
  613. currentTime = new_time;
  614. System.out.println("DDoS:");
  615. /**
  616. * 1 hour DDoS
  617. */
  618. Connection ddos = anomalyController.openDDosCreationMenu(broker, new LinkedList<SmartDevice>(zigbee.getDevices()));
  619. for(int i=25; i<26; i++)
  620. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  621. networkController.deleteConnection(ddos);
  622. new_time = System.currentTimeMillis();
  623. elapsed_time = new_time-currentTime;
  624. System.out.println("DDoS generation & classification: "+elapsed_time+"ms");
  625. currentTime = new_time;
  626. System.out.println("DoS:");
  627. /**
  628. * 1 hour DoS
  629. */
  630. Connection dos = anomalyController.runDosAttack(kitchenLight, kitchenFridge);
  631. for(int i=26; i<27; i++)
  632. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  633. networkController.deleteConnection(dos);
  634. new_time = System.currentTimeMillis();
  635. elapsed_time = new_time-currentTime;
  636. System.out.println("DoS generation & classification: "+elapsed_time+"ms");
  637. currentTime = new_time;
  638. /**
  639. * Value Anomaly 1h
  640. */
  641. System.out.println("Value Anomalies:");
  642. float min = kitchenFridge.getFSmin();
  643. float max = kitchenFridge.getFSmax();
  644. float val = kitchenFridge.getFSval();
  645. kitchenFridge.setFSmin(18);
  646. kitchenFridge.setFSval(18.5f);
  647. kitchenFridge.setFSmax(24);
  648. kitchenFridge.setLabel((short)-1);//-1 Value anomaly
  649. for(int i=27; i<28; i++)
  650. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  651. kitchenFridge.setFSmin(min);
  652. kitchenFridge.setFSmax(max);
  653. kitchenFridge.setFSval(val);
  654. kitchenFridge.setLabel((short)0);
  655. new_time = System.currentTimeMillis();
  656. elapsed_time = new_time-currentTime;
  657. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  658. currentTime = new_time;
  659. System.out.println("Testing completed");
  660. } catch(Exception e) {
  661. System.out.println("WARNING: Testing failed: ");
  662. e.printStackTrace();
  663. }
  664. }
  665. }