MenuBarNetworkExamples.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import java.awt.print.Paper;
  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.NetworkController;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SettingsController;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPerformance;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  22. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  23. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolCollectorDevice;
  24. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensorDevice;
  25. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatCollectorDevice;
  26. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensorDevice;
  27. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
  28. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.CountingMetric;
  29. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.Manipulation_RandomMove;
  30. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  31. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
  32. public class MenuBarNetworkExamples extends JMenu{
  33. /**
  34. *
  35. */
  36. private static final long serialVersionUID = 1L;
  37. /**
  38. * Controller controller
  39. */
  40. private Controller controller;
  41. /**
  42. * Controller for editing the network
  43. */
  44. private NetworkController networkController;
  45. /**
  46. * Controller for configuration of the program
  47. */
  48. private SettingsController settingsController;
  49. /**
  50. * SimulationController for running the simulation
  51. */
  52. private SimulationController simulationController;
  53. /**
  54. * Creates the JMenu for network examples in the MenuBar
  55. * @param controller controller to create examples
  56. */
  57. public MenuBarNetworkExamples(Controller controller) {
  58. super("Create example Network");
  59. this.controller = controller;
  60. this.networkController = controller.getNetworkController();
  61. this.settingsController = controller.getSettingsController();
  62. this.simulationController = controller.getSimulationController();
  63. /**
  64. * MQTT example
  65. */
  66. JMenuItem mnMQTT = new JMenuItem("MQTT Example");
  67. mnMQTT.addActionListener(a->initializeMQTTTest());
  68. this.add(mnMQTT);
  69. /**
  70. * Small Network Test
  71. */
  72. JMenuItem mnTest = new JMenuItem("Network Example");
  73. mnTest.addActionListener(a->initializeTest());
  74. this.add(mnTest);
  75. /**
  76. * Performance Comparison Example
  77. */
  78. JMenuItem mnPerformance = new JMenuItem("Performane Evaluation Preset");
  79. mnPerformance.addActionListener(a->initializePerformanceTest());
  80. this.add(mnPerformance);
  81. /**
  82. * User Study Preset
  83. */
  84. JMenuItem mnUserStudy = new JMenuItem("User Study Preset");
  85. mnUserStudy.addActionListener(a->initializeUserStudy());
  86. this.add(mnUserStudy);
  87. /**
  88. * Packet Collection example
  89. */
  90. JMenuItem mnPacketCollectionExmample = new JMenuItem("Packet Collection Example");
  91. mnPacketCollectionExmample.addActionListener(a->testPackageCollection());
  92. this.add(mnPacketCollectionExmample);
  93. /**
  94. * Paper example
  95. */
  96. JMenuItem mnPaperExample = new JMenuItem("Paper Example");
  97. mnPaperExample.addActionListener(a->createPaperExample());
  98. this.add(mnPaperExample);
  99. }
  100. private void initializePerformanceTest(){
  101. SmartDevice tic = new SmartDevice("Tic");
  102. tic.setX(100);
  103. tic.setY(100);
  104. SmartDevice toc = new SmartDevice("Toc");
  105. toc.setX(100);
  106. toc.setY(250);
  107. networkController.addSmartDevice(tic);
  108. networkController.addSmartDevice(toc);
  109. Link l = new PrecisionLink("Channel");
  110. networkController.addLink(l);
  111. networkController.addLinkToDevice(l, tic);
  112. networkController.addLinkToDevice(l, toc);
  113. Port ticP = new Port(tic, (short)2, 200, (short)0, 0, (short)0);
  114. Port tocP = new Port(toc, (short)3, 200, (short)0, -100, (short)0);
  115. tic.addPort(ticP);
  116. toc.addPort(tocP);
  117. Connection con = new ConnectionPrecision(l, new SimpleProtocol());
  118. networkController.addConnection(con);
  119. networkController.addConnectionToLink(con, l);
  120. networkController.addDeviceToConnectionAndProtocol(ticP, con, 0);
  121. networkController.addDeviceToConnectionAndProtocol(tocP, con, 1);
  122. simulationController.setStepDuration(100000000);
  123. simulationController.setEndTime(1000000000);
  124. controller.notifyObservers();
  125. }
  126. /**
  127. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  128. */
  129. private void initializeUserStudy(){
  130. /**
  131. * Preset one SmartDevice
  132. */
  133. SmartDevice A = new SmartDevice("Homestation");
  134. A.setX(200);
  135. A.setY(200);
  136. networkController.addSmartDevice(A);
  137. /**
  138. * Packagecollector, which is registered at the Device
  139. */
  140. PacketCollector collector = new PacketCollector(new CountingMetric());
  141. simulationController.getPacketCaptureController().addPacketCollector(collector);
  142. simulationController.getPacketCaptureController().addDeviceToCollector(collector, A);
  143. simulationController.setPrintPackets(true);
  144. controller.notifyObservers();
  145. }
  146. /**
  147. * Test package collectors
  148. */
  149. private void testPackageCollection() {
  150. simulationController.addAlgorithm(new Manipulation_RandomMove(), controller);
  151. PacketCollector collector = new PacketCollector();
  152. simulationController.getSimulationManager().getPacketCollectionManager().addPacketCollector(collector);
  153. System.out.println("Collector 0-500 - nothing collected:");
  154. simulationController.getSimulationManager().simulateTimeIntervall(0, 500);
  155. HashMap<Link, LinkedList<Packet>> map = collector.getPackets();
  156. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  157. System.out.println("Link: "+e.getKey().getName());
  158. for(Packet p : e.getValue())
  159. System.out.println(p.getTextualRepresentation());
  160. }
  161. System.out.println("");
  162. System.out.println("");
  163. Iterator<SmartDevice> it = networkController.getSmartDevices().iterator();
  164. it.next();
  165. SmartDevice d = it.next();
  166. collector.addDevice(d);
  167. SmartDevice f = it.next();
  168. collector.addDevice(f);
  169. System.out.println("Collector 500-1000 - "+d.getName()+" & "+f.getName()+" collected:");
  170. simulationController.getSimulationManager().simulateTimeIntervall(500, 500);
  171. map = collector.getPackets();
  172. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  173. System.out.println("Link: "+e.getKey().getName());
  174. for(Packet p : e.getValue())
  175. System.out.println(p.getTextualRepresentation());
  176. }
  177. System.out.println("");
  178. System.out.println("");
  179. Link l = networkController.getLinks().iterator().next();
  180. collector.addLink(l);
  181. System.out.println("Collector 2000-3000 - "+l+" collected:");
  182. simulationController.getSimulationManager().simulateTimeIntervall(1000, 500);
  183. map = collector.getPackets();
  184. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  185. System.out.println("Link: "+e.getKey().getName());
  186. for(Packet p : e.getValue())
  187. System.out.println(p.getTextualRepresentation());
  188. }
  189. simulationController.resetSimulation();
  190. controller.notifyObservers();
  191. }
  192. /**
  193. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  194. */
  195. private void initializeTest(){
  196. SmartDevice A = null, B = null, C = null;
  197. for(int i = 0; i<5; i++){
  198. A = new SmartDevice("SmartTV"+i);
  199. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  200. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  201. B = new SmartDevice("SmartDoor"+i);
  202. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  203. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  204. C = new SmartDevice("SmartLight"+i);
  205. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  206. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  207. networkController.addSmartDevice(A);
  208. networkController.addSmartDevice(B);
  209. networkController.addSmartDevice(C);
  210. }
  211. Link link = new SimpleLink("SimpleWifi");
  212. link.addDevice(A);
  213. link.addDevice(B);
  214. link.addDevice(C);
  215. A.addLink(link);
  216. B.addLink(link);
  217. C.addLink(link);
  218. Port a = new Port(A, (short) 1);
  219. a.setLastTrigger(0);
  220. a.setTriggerInterval(68);
  221. a.setStatus(Port.SENDING);
  222. A.addPort(a);
  223. Port b = new Port(B, (short) 2);
  224. b.setTriggerInterval(102);
  225. b.setStatus(Port.SENDING);
  226. B.addPort(b);
  227. Connection s = new ConnectionPerformance(link, new SimpleProtocol(a, b));
  228. s.setPacketLossProbability(0.01);//1% Packet loss probability
  229. a.setConnection(s);
  230. b.setConnection(s);
  231. s.addSmartDevice(a);
  232. s.addSmartDevice(b);
  233. link.addConnection(s);
  234. networkController.addLink(link);
  235. networkController.addConnection(s);
  236. controller.notifyObservers();
  237. }
  238. /**
  239. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  240. */
  241. private void initializeMQTTTest(){
  242. SimpleLink link = new SimpleLink("WIFI");
  243. link.setFixedDelay(5);
  244. SmartDevice broker = new SmartDevice("MQTT-Broker");
  245. broker.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  246. broker.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  247. networkController.addSmartDevice(broker);
  248. Port brokerPort = new Port(broker, (short) 0);
  249. brokerPort.setLastTrigger(0);
  250. brokerPort.setTriggerInterval(987);
  251. brokerPort.setStatus(Port.OPEN);
  252. broker.addPort(brokerPort);
  253. link.addDevice(broker);
  254. broker.addLink(link);
  255. Protocol protocol = new MQTT_protocol(brokerPort);
  256. Connection con = new ConnectionPrecision(link, protocol);
  257. con.setPacketLossProbability(0.01);//1% Packet loss probability
  258. con.addSmartDevice(brokerPort);
  259. brokerPort.setConnection(con);
  260. con.setStatus(Connection.ACTIVE);
  261. networkController.addLink(link);
  262. link.addConnection(con);
  263. networkController.addConnection(con);
  264. SmartDevice A = null, A1 = null, A2 = null, B = null, C = null;
  265. Port aP,a1P,a2P,bP,cP;
  266. for(int i = 0; i<3; i++){
  267. FloatSensorDevice aS = new FloatSensorDevice("SmartThermostat"+i+"(Pub)");
  268. aS.setFSinfoName("room"+i+"/temperature");
  269. A=aS;
  270. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  271. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  272. link.addDevice(A);
  273. A.addLink(link);
  274. aP = new Port(A,((short) (3*i+1)));
  275. aP.setLastTrigger(0);
  276. aP.setTriggerInterval(100+(int)(Math.random()*900));
  277. aP.setJitter((short) (Math.random()*50));
  278. aP.setStatus(Port.SENDING);
  279. aP.setConnection(con);
  280. protocol.addDeviceOfRole(aP, 2);
  281. con.addSmartDevice(aP);
  282. A.addPort(aP);
  283. networkController.addSmartDevice(A);
  284. FloatCollectorDevice aS1 = new FloatCollectorDevice("SmartAirCondition"+i+"(Sub)");
  285. aS1.setFCinfoName("room"+i+"/temperature");
  286. A1=aS1;
  287. A1.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  288. A1.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  289. link.addDevice(A1);
  290. A1.addLink(link);
  291. a1P = new Port(A1,((short) (3*i+1)));
  292. a1P.setLastTrigger(0);
  293. a1P.setTriggerInterval(100+(int)(Math.random()*900));
  294. a1P.setJitter((short) (Math.random()*50));
  295. a1P.setStatus(Port.SENDING);
  296. a1P.setConnection(con);
  297. protocol.addDeviceOfRole(a1P, 3);
  298. con.addSmartDevice(a1P);
  299. A1.addPort(a1P);
  300. networkController.addSmartDevice(A1);
  301. FloatCollectorDevice aS2 = new FloatCollectorDevice("SmartHeater"+i+"(Sub)");
  302. aS2.setFCinfoName("room"+i+"/temperature");
  303. A2=aS2;
  304. A2.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  305. A2.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  306. link.addDevice(A2);
  307. A2.addLink(link);
  308. a2P = new Port(A2,((short) (3*i+1)));
  309. a2P.setLastTrigger(0);
  310. a2P.setTriggerInterval(100+(int)(Math.random()*900));
  311. a2P.setJitter((short) (Math.random()*50));
  312. a2P.setStatus(Port.SENDING);
  313. a2P.setConnection(con);
  314. protocol.addDeviceOfRole(a2P, 3);
  315. con.addSmartDevice(a2P);
  316. A2.addPort(a2P);
  317. networkController.addSmartDevice(A2);
  318. B = new BoolSensorDevice("SmartDoor"+i+"(Pub)");
  319. ((BoolSensorDevice)B).setBSinfoName("room"+i+"/DoorOpen");
  320. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  321. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  322. link.addDevice(B);
  323. B.addLink(link);
  324. bP = new Port(B,((short) (3*i+2)));
  325. bP.setLastTrigger(0);
  326. bP.setTriggerInterval(10+(int)(Math.random()*190));
  327. bP.setJitter((short) (Math.random()*50));
  328. bP.setStatus(Port.SENDING);
  329. bP.setConnection(con);
  330. protocol.addDeviceOfRole(bP, 2);
  331. con.addSmartDevice(bP);
  332. B.addPort(bP);
  333. networkController.addSmartDevice(B);
  334. C = new BoolCollectorDevice("SmartDoorStatusLight"+i+"(Sub)");
  335. ((BoolCollectorDevice)C).setBCinfoName("room"+i+"/DoorOpen");
  336. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  337. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  338. link.addDevice(C);
  339. C.addLink(link);
  340. cP = new Port(C,((short) (3*i+1)));
  341. cP.setLastTrigger(0);
  342. cP.setTriggerInterval(50+(int)(Math.random()*450));
  343. cP.setJitter((short) (Math.random()*50));
  344. cP.setStatus(Port.SENDING);
  345. cP.setConnection(con);
  346. protocol.addDeviceOfRole(cP, 3);
  347. con.addSmartDevice(cP);
  348. C.addPort(cP);
  349. networkController.addSmartDevice(C);
  350. controller.notifyObservers();
  351. }
  352. }
  353. public void createPaperExample(){
  354. /*
  355. * Main networking devices
  356. */
  357. SmartDevice router = networkController.createSmartDevice("Wifi-Router", 500, 100, 50);
  358. SmartDevice zigBeeRouter = networkController.createSmartDevice("ZigBee-Router", 500, 300, 50);
  359. SmartDevice broker = networkController.createSmartDevice("Broker", 500, 500, 50);
  360. /*
  361. * Links/Networks
  362. */
  363. Link wifi = new PrecisionLink("Wifi");
  364. networkController.addLink(wifi);
  365. Link zigbee = new PrecisionLink("ZigBee");
  366. networkController.addLink(zigbee);
  367. /*
  368. * Connect Devices to Links
  369. */
  370. networkController.addLinkToDevice(wifi, router);
  371. networkController.addLinkToDevice(wifi, zigBeeRouter);
  372. networkController.addLinkToDevice(zigbee, zigBeeRouter);
  373. networkController.addLinkToDevice(zigbee, broker);
  374. /*
  375. * Internet Access Connection
  376. */
  377. Connection inetAcces = new ConnectionPrecision();
  378. inetAcces.setName("Cloud Access");
  379. networkController.addConnectionToLink(inetAcces, wifi);
  380. inetAcces.setProtocol(new SimpleProtocol());
  381. Port pRouter = new Port(router, (short)80, 1000L);
  382. pRouter.setTriggerInterval(-500L);
  383. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  384. Port pZigBee = new Port(zigBeeRouter, (short)80, 1000L);
  385. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  386. networkController.addDeviceToConnectionAndProtocol(pZigBee, inetAcces, 1);
  387. networkController.addConnection(inetAcces);
  388. /*
  389. * Create MQTT Connection
  390. */
  391. Connection mqtt = new ConnectionPrecision();
  392. mqtt.setName("Automation (MQTT)");
  393. networkController.addConnectionToLink(mqtt, zigbee);
  394. mqtt.setProtocol(new MQTT_protocol());
  395. Port pBroker = new Port(broker, (short)1883);
  396. pBroker.setStatus(Port.OPEN);
  397. pBroker.setResponseTime((short)2);
  398. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  399. networkController.addConnection(mqtt);
  400. /*
  401. * Add some MQTT Devices
  402. */
  403. FloatSensorDevice floatSensor = new FloatSensorDevice("Thermostat 1");
  404. floatSensor.setFSinfoName("home/kitchen/temperature");
  405. networkController.addLinkToDevice(zigbee, floatSensor);
  406. networkController.moveSmartDevice(floatSensor, 300, 500, 50);
  407. floatSensor.setFSmin(15.0f);
  408. floatSensor.setFSmax(32.0f);
  409. networkController.addSmartDevice(floatSensor);
  410. Port pFloatSensor = new Port(floatSensor, (short)1883);
  411. pFloatSensor.setStatus(Port.SENDING);
  412. pFloatSensor.setLastTrigger(-357L);
  413. pFloatSensor.setTriggerInterval(15000);//15sek
  414. networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
  415. /*
  416. * Update visualization
  417. */
  418. controller.notifyObservers();
  419. }
  420. }