123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Map.Entry;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SettingsController;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPerformance;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolCollectorDevice;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensorDevice;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatCollectorDevice;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensorDevice;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.CountingMetric;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.Manipulation_RandomMove;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
- public class MenuBarNetworkExamples extends JMenu{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- /**
- * Controller controller
- */
- private Controller controller;
-
- /**
- * Controller for editing the network
- */
- private NetworkController networkController;
-
- /**
- * Controller for configuration of the program
- */
- private SettingsController settingsController;
-
- /**
- * SimulationController for running the simulation
- */
- private SimulationController simulationController;
- /**
- * Creates the JMenu for network examples in the MenuBar
- * @param controller controller to create examples
- */
- public MenuBarNetworkExamples(Controller controller) {
- super("Create example Network");
-
- this.controller = controller;
- this.networkController = controller.getNetworkController();
- this.settingsController = controller.getSettingsController();
- this.simulationController = controller.getSimulationController();
-
- /**
- * MQTT example
- */
- JMenuItem mnMQTT = new JMenuItem("MQTT Example");
- mnMQTT.addActionListener(a->initializeMQTTTest());
- this.add(mnMQTT);
-
- /**
- * Small Network Test
- */
- JMenuItem mnTest = new JMenuItem("Network Example");
- mnTest.addActionListener(a->initializeTest());
- this.add(mnTest);
-
- /**
- * Performance Comparison Example
- */
- JMenuItem mnPerformance = new JMenuItem("Performane Evaluation Preset");
- mnPerformance.addActionListener(a->initializePerformanceTest());
- this.add(mnPerformance);
-
- /**
- * User Study Preset
- */
- JMenuItem mnUserStudy = new JMenuItem("User Study Preset");
- mnUserStudy.addActionListener(a->initializeUserStudy());
- this.add(mnUserStudy);
-
- /**
- * Packet Collection example
- */
- JMenuItem mnPacketCollectionExmample = new JMenuItem("Packet Collection Example");
- mnPacketCollectionExmample.addActionListener(a->testPackageCollection());
- this.add(mnPacketCollectionExmample);
-
- /**
- * Paper example
- */
- JMenuItem mnPaperExample = new JMenuItem("Paper Example");
- mnPaperExample.addActionListener(a->createPaperExample());
- this.add(mnPaperExample);
- }
-
- private void initializePerformanceTest(){
- SmartDevice tic = new SmartDevice("Tic");
- tic.setX(100);
- tic.setY(100);
- SmartDevice toc = new SmartDevice("Toc");
- toc.setX(100);
- toc.setY(250);
- networkController.addSmartDevice(tic);
- networkController.addSmartDevice(toc);
-
- Link l = new PrecisionLink("Channel");
- networkController.addLink(l);
- networkController.addLinkToDevice(l, tic);
- networkController.addLinkToDevice(l, toc);
- Port ticP = new Port(tic, (short)2, 200, (short)0, 0, (short)0);
- Port tocP = new Port(toc, (short)3, 200, (short)0, -100, (short)0);
- tic.addPort(ticP);
- toc.addPort(tocP);
-
- Connection con = new ConnectionPrecision(l, new SimpleProtocol());
- networkController.addConnection(con);
- networkController.addConnectionToLink(con, l);
- networkController.addDeviceToConnectionAndProtocol(ticP, con, 0);
- networkController.addDeviceToConnectionAndProtocol(tocP, con, 1);
-
- simulationController.setStepDuration(100000000);
- simulationController.setEndTime(1000000000);
- controller.notifyObservers();
- }
-
- /**
- * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
- */
- private void initializeUserStudy(){
- /**
- * Preset one SmartDevice
- */
- SmartDevice A = new SmartDevice("Homestation");
- A.setX(200);
- A.setY(200);
- networkController.addSmartDevice(A);
- /**
- * Packagecollector, which is registered at the Device
- */
- PacketCollector collector = new PacketCollector(new CountingMetric());
- simulationController.getPacketCaptureController().addPacketCollector(collector);
- simulationController.getPacketCaptureController().addDeviceToCollector(collector, A);
- simulationController.setPrintPackets(true);
- controller.notifyObservers();
- }
-
- /**
- * Test package collectors
- */
- private void testPackageCollection() {
- simulationController.addAlgorithm(new Manipulation_RandomMove(), controller);
- PacketCollector collector = new PacketCollector();
- simulationController.getSimulationManager().getPacketCollectionManager().addPacketCollector(collector);
-
- System.out.println("Collector 0-500 - nothing collected:");
- simulationController.getSimulationManager().simulateTimeIntervall(0, 500);
- HashMap<Link, LinkedList<Packet>> map = collector.getPackets();
- for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
- System.out.println("Link: "+e.getKey().getName());
- for(Packet p : e.getValue())
- System.out.println(p.getTextualRepresentation());
- }
-
- System.out.println("");
- System.out.println("");
- Iterator<SmartDevice> it = networkController.getSmartDevices().iterator();
- it.next();
-
- SmartDevice d = it.next();
- collector.addDevice(d);
- SmartDevice f = it.next();
- collector.addDevice(f);
- System.out.println("Collector 500-1000 - "+d.getName()+" & "+f.getName()+" collected:");
- simulationController.getSimulationManager().simulateTimeIntervall(500, 500);
- map = collector.getPackets();
- for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
- System.out.println("Link: "+e.getKey().getName());
- for(Packet p : e.getValue())
- System.out.println(p.getTextualRepresentation());
- }
- System.out.println("");
- System.out.println("");
-
- Link l = networkController.getLinks().iterator().next();
- collector.addLink(l);
- System.out.println("Collector 2000-3000 - "+l+" collected:");
- simulationController.getSimulationManager().simulateTimeIntervall(1000, 500);
- map = collector.getPackets();
- for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
- System.out.println("Link: "+e.getKey().getName());
- for(Packet p : e.getValue())
- System.out.println(p.getTextualRepresentation());
- }
- simulationController.resetSimulation();
- controller.notifyObservers();
- }
-
- /**
- * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
- */
- private void initializeTest(){
- SmartDevice A = null, B = null, C = null;
- for(int i = 0; i<5; i++){
- A = new SmartDevice("SmartTV"+i);
- A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- B = new SmartDevice("SmartDoor"+i);
- B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- C = new SmartDevice("SmartLight"+i);
- C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
-
- networkController.addSmartDevice(A);
- networkController.addSmartDevice(B);
- networkController.addSmartDevice(C);
- }
-
- Link link = new SimpleLink("SimpleWifi");
- link.addDevice(A);
- link.addDevice(B);
- link.addDevice(C);
- A.addLink(link);
- B.addLink(link);
- C.addLink(link);
-
- Port a = new Port(A, (short) 1, 68);
- a.setLastTrigger(0);
- a.setStatus(Port.SENDING);
- A.addPort(a);
- Port b = new Port(B, (short) 2, 102);
- b.setStatus(Port.SENDING);
- B.addPort(b);
-
- Connection s = new ConnectionPerformance(link, new SimpleProtocol(a, b));
- s.setPacketLossProbability(0.01);//1% Packet loss probability
- a.setConnection(s);
- b.setConnection(s);
- s.addSmartDevice(a);
- s.addSmartDevice(b);
- link.addConnection(s);
-
- networkController.addLink(link);
- networkController.addConnection(s);
- controller.notifyObservers();
- }
-
- /**
- * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
- */
- private void initializeMQTTTest(){
- SimpleLink link = new SimpleLink("WIFI");
- link.setFixedDelay(5);
-
- SmartDevice broker = new SmartDevice("MQTT-Broker");
- broker.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- broker.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- networkController.addSmartDevice(broker);
-
- Port brokerPort = new Port(broker, (short) 0, 987);
- brokerPort.setLastTrigger(0);
- brokerPort.setStatus(Port.OPEN);
- broker.addPort(brokerPort);
-
- link.addDevice(broker);
- broker.addLink(link);
-
- Protocol protocol = new MQTT_protocol(brokerPort);
-
- Connection con = new ConnectionPrecision(link, protocol);
- con.setPacketLossProbability(0.01);//1% Packet loss probability
- con.addSmartDevice(brokerPort);
- brokerPort.setConnection(con);
- con.setStatus(Connection.ACTIVE);
- networkController.addLink(link);
- link.addConnection(con);
- networkController.addConnection(con);
-
- SmartDevice A = null, A1 = null, A2 = null, B = null, C = null;
- Port aP,a1P,a2P,bP,cP;
- for(int i = 0; i<3; i++){
- FloatSensorDevice aS = new FloatSensorDevice("SmartThermostat"+i+"(Pub)");
- aS.setFSinfoName("room"+i+"/temperature");
- A=aS;
- A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- link.addDevice(A);
- A.addLink(link);
-
- aP = new Port(A,((short) (3*i+1)),100+(int)(Math.random()*900));
- aP.setLastTrigger(0);
- aP.setJitter((short) (Math.random()*50));
- aP.setStatus(Port.SENDING);
- aP.setConnection(con);
- protocol.addDeviceOfRole(aP, 2);
- con.addSmartDevice(aP);
- A.addPort(aP);
-
- networkController.addSmartDevice(A);
-
-
- FloatCollectorDevice aS1 = new FloatCollectorDevice("SmartAirCondition"+i+"(Sub)");
- aS1.setFCinfoName("room"+i+"/temperature");
- A1=aS1;
- A1.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- A1.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- link.addDevice(A1);
- A1.addLink(link);
-
- a1P = new Port(A1,((short) (3*i+1)),100+(int)(Math.random()*900));
- a1P.setLastTrigger(0);
- a1P.setJitter((short) (Math.random()*50));
- a1P.setStatus(Port.SENDING);
- a1P.setConnection(con);
- protocol.addDeviceOfRole(a1P, 3);
- con.addSmartDevice(a1P);
- A1.addPort(a1P);
-
- networkController.addSmartDevice(A1);
-
-
- FloatCollectorDevice aS2 = new FloatCollectorDevice("SmartHeater"+i+"(Sub)");
- aS2.setFCinfoName("room"+i+"/temperature");
- A2=aS2;
- A2.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- A2.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- link.addDevice(A2);
- A2.addLink(link);
-
- a2P = new Port(A2,((short) (3*i+1)),100+(int)(Math.random()*900));
- a2P.setLastTrigger(0);
- a2P.setJitter((short) (Math.random()*50));
- a2P.setStatus(Port.SENDING);
- a2P.setConnection(con);
- protocol.addDeviceOfRole(a2P, 3);
- con.addSmartDevice(a2P);
- A2.addPort(a2P);
-
- networkController.addSmartDevice(A2);
-
-
-
-
- B = new BoolSensorDevice("SmartDoor"+i+"(Pub)");
- ((BoolSensorDevice)B).setBSinfoName("room"+i+"/DoorOpen");
- B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- link.addDevice(B);
- B.addLink(link);
-
- bP = new Port(B,((short) (3*i+2)),10+(int)(Math.random()*190));
- bP.setLastTrigger(0);
- bP.setJitter((short) (Math.random()*50));
- bP.setStatus(Port.SENDING);
- bP.setConnection(con);
- protocol.addDeviceOfRole(bP, 2);
- con.addSmartDevice(bP);
- B.addPort(bP);
-
- networkController.addSmartDevice(B);
-
- C = new BoolCollectorDevice("SmartDoorStatusLight"+i+"(Sub)");
- ((BoolCollectorDevice)C).setBCinfoName("room"+i+"/DoorOpen");
- C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
- link.addDevice(C);
- C.addLink(link);
-
- cP = new Port(C,((short) (3*i+1)), 50+(int)(Math.random()*450));
- cP.setLastTrigger(0);
- cP.setJitter((short) (Math.random()*50));
- cP.setStatus(Port.SENDING);
- cP.setConnection(con);
- protocol.addDeviceOfRole(cP, 3);
- con.addSmartDevice(cP);
- C.addPort(cP);
-
- networkController.addSmartDevice(C);
- controller.notifyObservers();
- }
- }
-
- public void createPaperExample(){
- /*
- * Main networking devices
- */
- SmartDevice router = networkController.createSmartDevice("Wifi-Router", 500, 100, 50);
- SmartDevice zigBeeRouter = networkController.createSmartDevice("ZigBee-Router", 500, 300, 50);
- SmartDevice broker = networkController.createSmartDevice("Broker", 500, 500, 50);
-
- /*
- * Links/Networks
- */
- Link wifi = new PrecisionLink("Wifi");
- networkController.addLink(wifi);
- Link zigbee = new PrecisionLink("ZigBee");
- networkController.addLink(zigbee);
-
- /*
- * Connect Devices to Links
- */
- networkController.addLinkToDevice(wifi, router);
- networkController.addLinkToDevice(wifi, zigBeeRouter);
- networkController.addLinkToDevice(zigbee, zigBeeRouter);
- networkController.addLinkToDevice(zigbee, broker);
-
- /*
- * Internet Access Connection
- */
- Connection inetAcces = new ConnectionPrecision();
- inetAcces.setName("Cloud Access");
- networkController.addConnectionToLink(inetAcces, wifi);
- inetAcces.setProtocol(new SimpleProtocol());
- Port pRouter = new Port(router, (short)80, 500L);
- networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
- Port pZigBee = new Port(zigBeeRouter, (short)80, 1000L);
- networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
- networkController.addDeviceToConnectionAndProtocol(pZigBee, inetAcces, 1);
- networkController.addConnection(inetAcces);
-
-
- /*
- * Create MQTT Connection
- */
- Connection mqtt = new ConnectionPrecision();
- mqtt.setName("Automation (MQTT)");
- networkController.addConnectionToLink(mqtt, zigbee);
- mqtt.setProtocol(new MQTT_protocol());
- Port pBroker = new Port(broker, (short)1883);
- pBroker.setStatus(Port.OPEN);
- pBroker.setResponseTime((short)2);
- networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
- networkController.addConnection(mqtt);
-
- /*
- * Add some MQTT Devices
- */
- FloatSensorDevice floatSensor = new FloatSensorDevice("Thermostat 1");
- floatSensor.setFSinfoName("home/kitchen/temperature");
- networkController.addLinkToDevice(zigbee, floatSensor);
- networkController.moveSmartDevice(floatSensor, 300, 500, 50);
- floatSensor.setFSmin(15.0f);
- floatSensor.setFSmax(32.0f);
- networkController.addSmartDevice(floatSensor);
- Port pFloatSensor = new Port(floatSensor, (short)1883, 15000);
- pFloatSensor.setStatus(Port.SENDING);
- pFloatSensor.setLastTrigger(-357L);
- networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
-
-
- /*
- * Update visualization
- */
- controller.notifyObservers();
- }
- }
|