Kaynağa Gözat

Adds Simulation Window to start, stop, configure the simulation

Andreas T. Meyer-Berg 5 yıl önce
ebeveyn
işleme
0fbc8b78c4

+ 3 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -49,11 +49,12 @@ public class Main {
 		m = new Model();
 		c = new Controller(m);
 	    v = new MainFrame(m, c);
-	    sim = new SimulationManager(m);
 	    //initializeTest();
 	    initializeMQTTTest();
+	    /*
 	    for(int i=0; i<10; i++)
-	    	sim.simulateTimeIntervall(0+50*i, 50);
+	    	m.getSim().simulateTimeIntervall(0+50*i, 50);
+	    */
 	}
 	
 	/**

+ 20 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Model.java

@@ -37,11 +37,16 @@ public class Model extends Observable{
 	 */
 	private int device_visualization_radius = 17;
 	
+	/**
+	 * Simulation Manager which allows simulation of this model
+	 */
+	private SimulationManager sim;
+	
 	/**
 	 * Initializes the Model, with 3 default devices for testing purposes
 	 */
 	public Model() {
-		
+		setSim(new SimulationManager(this));
 		setWidth(640);
 		setHeight(480);
 		setDepth(480);
@@ -153,4 +158,18 @@ public class Model extends Observable{
 	public void setChanged(){
 		super.setChanged();
 	}
+
+	/**
+	 * @return the sim
+	 */
+	public SimulationManager getSim() {
+		return sim;
+	}
+
+	/**
+	 * @param sim the sim to set
+	 */
+	public void setSim(SimulationManager sim) {
+		this.sim = sim;
+	}
 }

+ 26 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -6,8 +6,14 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  * @author Andreas T. Meyer-Berg
  */
 public class SimulationManager {
+	/**
+	 * Model which should be simulated
+	 */
 	Model model;
-	long stepDuration = 100;
+	/**
+	 * True if packets should be printed
+	 */
+	boolean printPackets = false;
 	/**
 	 * Creates a new Simulationmanager
 	 * 
@@ -32,7 +38,8 @@ public class SimulationManager {
 		//Simulate SmartDevices - if they need some logic
 		model.getDevices().forEach(d -> d.simulateTimeStep(startTime, duration));
 		//Store Packages/Export Packages etc. (for debug purposes)
-		model.getConnectionNetworks().forEach(d->d.getPackets()
+		if(printPackets)
+			model.getConnectionNetworks().forEach(d->d.getPackets()
 				.forEach(p-> 
 				{
 					if(p == null)
@@ -43,4 +50,21 @@ public class SimulationManager {
 		
 		
 	}
+
+	/**
+	 * Sets Print Packets, if true, the simulation will print the packets
+	 * @param printPackets true if simulation should print the packets
+	 */
+	public void setPrintPackets(boolean printPackets) {
+		this.printPackets = printPackets;
+	}
+
+	/**
+	 * Returns true if the simulations will print the packets
+	 * 
+	 * @return if the packets are printed
+	 */
+	public boolean getPrintPackets() {
+		return printPackets;
+	}
 }

+ 10 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MainFrame.java

@@ -78,10 +78,19 @@ public class MainFrame extends JFrame {
 		
 		JMenuBar menuBar = new JMenuBar();
 		getContentPane().add(menuBar, BorderLayout.NORTH);
-		
+		/*
 		JMenuItem mntmOptions = new JMenuItem("Options");
 		mntmOptions.setHorizontalAlignment(SwingConstants.LEFT);
 		menuBar.add(mntmOptions);
+		*/
+		JMenuItem mntmSimulation = new JMenuItem("Simulation");
+		mntmSimulation.setHorizontalAlignment(SwingConstants.LEFT);
+		mntmSimulation.addActionListener(a->{
+			SimulationConfigurator sim = new SimulationConfigurator(m.getSim());
+			sim.setLocationRelativeTo(this);
+			sim.setVisible(true);
+		});
+		menuBar.add(mntmSimulation);
 		
 		this.setVisible(true);
 		//Set Dimension of the model to the JPanel size

+ 156 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/SimulationConfigurator.java

@@ -0,0 +1,156 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import javax.swing.JFrame;
+import javax.swing.JProgressBar;
+import javax.swing.JButton;
+import javax.swing.JLabel;
+import javax.swing.JTextField;
+import javax.swing.Timer;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
+
+import java.awt.Rectangle;
+import javax.swing.JCheckBox;
+
+@SuppressWarnings("serial")
+public class SimulationConfigurator extends JFrame {
+	private JTextField tfStartTimeLong;
+	private JTextField tfStepDuration;
+	private JTextField tfEndTimeLong;
+	private JLabel lbCurrentTimeShow;
+	private JProgressBar progressBar; 
+	private Timer timer;
+	private SimulationManager sim;
+	private long currentTime = 0;
+	private long startTime = 0;
+	private long duration = 100;
+	private long endTime = 0;
+	
+	public SimulationConfigurator(SimulationManager sim) {
+		this.sim = sim;
+		setBounds(new Rectangle(0, 0, 460, 300));
+		setResizable(false);
+		getContentPane().setLayout(null);
+		
+		JButton btnStartStop = new JButton("Start Simulation");
+		btnStartStop.setBounds(12, 189, 149, 37);
+		getContentPane().add(btnStartStop);
+		btnStartStop.addActionListener(a->startStopButtonAction());
+		
+		JButton btnReset = new JButton("Reset");
+		btnReset.setBounds(173, 189, 123, 37);
+		getContentPane().add(btnReset);
+		btnReset.addActionListener(a->resetAction());
+		
+		JButton btnChooseStartDate = new JButton("Choose Start Date");
+		btnChooseStartDate.setBounds(12, 13, 161, 25);
+		getContentPane().add(btnChooseStartDate);
+		
+		JButton btnStartTime = new JButton("Choose End Date");
+		btnStartTime.setBounds(271, 13, 161, 25);
+		getContentPane().add(btnStartTime);
+		
+		JLabel lblStarttimeLong = new JLabel("StartTime (long):");
+		lblStarttimeLong.setBounds(12, 70, 128, 16);
+		getContentPane().add(lblStarttimeLong);
+		
+		JLabel lblStepDuration = new JLabel("Step duration:");
+		lblStepDuration.setBounds(12, 99, 97, 16);
+		getContentPane().add(lblStepDuration);
+		
+		tfStartTimeLong = new JTextField();
+		tfStartTimeLong.setText("0");
+		tfStartTimeLong.setBounds(116, 67, 116, 22);
+		getContentPane().add(tfStartTimeLong);
+		tfStartTimeLong.setColumns(10);
+		
+		tfStepDuration = new JTextField();
+		tfStepDuration.setText("1000");
+		tfStepDuration.setBounds(116, 99, 116, 22);
+		getContentPane().add(tfStepDuration);
+		tfStepDuration.setColumns(10);
+		
+		JLabel lblEndtimeLong = new JLabel("EndTime (long):");
+		lblEndtimeLong.setBounds(244, 70, 93, 16);
+		getContentPane().add(lblEndtimeLong);
+		
+		tfEndTimeLong = new JTextField();
+		tfEndTimeLong.setText("1000");
+		tfEndTimeLong.setBounds(347, 67, 85, 22);
+		getContentPane().add(tfEndTimeLong);
+		tfEndTimeLong.setColumns(10);
+		
+		JLabel lblCurrentTime = new JLabel("Current Time:");
+		lblCurrentTime.setBounds(12, 160, 97, 16);
+		getContentPane().add(lblCurrentTime);
+		
+		lbCurrentTimeShow = new JLabel("0 ms");
+		lbCurrentTimeShow.setBounds(121, 160, 111, 16);
+		getContentPane().add(lbCurrentTimeShow);
+		
+		progressBar = new JProgressBar();
+		progressBar.setBounds(0, 251, 442, 14);
+		getContentPane().add(progressBar);
+		progressBar.setValue(0);
+		
+		JCheckBox chckbxPrintpackets = new JCheckBox("printPackets");
+		chckbxPrintpackets.setBounds(116, 127, 113, 25);
+		chckbxPrintpackets.setSelected(sim.getPrintPackets());
+		getContentPane().add(chckbxPrintpackets);
+		chckbxPrintpackets.addActionListener(l->sim.setPrintPackets(chckbxPrintpackets.isSelected()));
+	}
+	
+	private void startStopButtonAction(){
+		if(timer == null){
+			timer = new Timer(0, a->simulateTimeStep());
+			currentTime = Long.parseLong(tfStartTimeLong.getText());
+			endTime = Long.parseLong(tfEndTimeLong.getText());
+			startTime = Long.parseLong(tfStartTimeLong.getText());
+			duration = Long.parseLong(tfStepDuration.getText());
+			progressBar.setMinimum((int) Long.parseLong(tfStartTimeLong.getText()));
+			progressBar.setMaximum((int) Long.parseLong(tfEndTimeLong.getText()));
+			progressBar.setValue((int) Long.parseLong(tfStartTimeLong.getText()));
+			lbCurrentTimeShow.setText(currentTime+" ms");
+		}
+		if(timer.isRunning()){
+			timer.stop();
+		}else{
+			timer.start();
+		}
+	}
+	
+	private void resetAction(){
+		timer.stop();
+		try {
+			currentTime = Long.parseLong(tfStartTimeLong.getText());
+			endTime = Long.parseLong(tfEndTimeLong.getText());
+			startTime = Long.parseLong(tfStartTimeLong.getText());
+			duration = Long.parseLong(tfStepDuration.getText());
+			progressBar.setMinimum((int) startTime);
+			progressBar.setMaximum((int) endTime);
+			progressBar.setValue((int) startTime);
+			lbCurrentTimeShow.setText(currentTime+"");
+		} catch (Exception e) {
+			// TODO: handle exception
+		}
+	}
+	
+	private void simulateTimeStep(){
+		if(currentTime>endTime){
+			timer.stop();
+			return;
+		}
+		sim.simulateTimeIntervall(currentTime,duration);
+		currentTime += duration;
+		progressBar.setValue((int) currentTime);
+		lbCurrentTimeShow.setText(currentTime+"");
+	}
+	
+	public static void main(String[] args) {
+		SimulationConfigurator simConfig = new SimulationConfigurator(new SimulationManager(new Model()));
+		simConfig.setEnabled(true);
+		simConfig.setVisible(true);
+		
+	}
+}