Browse Source

Merge remote-tracking branch 'refs/remotes/origin/E-worldEnergy&Water' into E-worldEnergy&Water

egert.rolf@tk.tu-darmstadt.de 5 years ago
parent
commit
e8e87b028a

+ 219 - 0
src/connection/ConnectHandheld.java

@@ -0,0 +1,219 @@
+package connection;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.net.BindException;
+import java.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JFormattedTextField;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JSplitPane;
+import javax.swing.JTextArea;
+import javax.swing.text.NumberFormatter;
+
+import api.Algorithm;
+import classes.AbstractCpsObject;
+import classes.CpsUpperNode;
+import classes.HolonObject;
+import connection.ConnectPhysical.HolonObjectStatus;
+import connection.ConnectPhysical.PhysicalLinkWrapper;
+import connection.socket.Server;
+import ui.controller.Control;
+import ui.view.Console;
+
+public class ConnectHandheld implements Algorithm{
+	
+	//Holeg
+	Control control;
+	private HolonObject observed;
+	
+	//Gui
+	private JPanel content = new JPanel();
+	private JTextArea textArea;
+	
+
+	//TCP
+	int port = 4242;
+	Server server;
+	
+	private Console console;
+	private boolean holonObjectConnected;
+	private JLabel houseLabel;
+	
+
+
+
+
+	public static void main(String[] args)
+	{
+	      JFrame newFrame = new JFrame("exampleWindow");
+	      ConnectHandheld instance = new ConnectHandheld();
+	      newFrame.setContentPane(instance.getAlgorithmPanel());
+	      newFrame.pack();
+	      newFrame.setVisible(true);
+	      newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+	}
+
+	
+	
+	
+	
+	public ConnectHandheld() {
+		content.setLayout(new BorderLayout());
+		
+		textArea = new JTextArea();
+		textArea.setEditable(false);
+		console = new Console();
+		JScrollPane scrollPane = new JScrollPane(console);
+		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
+				createSettingsPanel() , scrollPane);
+		splitPane.setResizeWeight(0.0);
+		content.add(splitPane, BorderLayout.CENTER);
+		content.setPreferredSize(new Dimension(400,600));	
+	}
+
+
+	
+	private JPanel createSettingsPanel() {
+		JPanel settingsPanel = new JPanel(null);
+		settingsPanel.setPreferredSize(new Dimension(400, 400));
+		settingsPanel.setMinimumSize(new Dimension(400, 225));
+		
+		NumberFormat format = NumberFormat.getIntegerInstance();
+		format.setGroupingUsed(false);
+		format.setParseIntegerOnly(true);
+		NumberFormatter integerFormatter = new NumberFormatter(format);
+		integerFormatter.setMinimum(0);
+		integerFormatter.setMaximum(65535);
+		integerFormatter.setCommitsOnValidEdit(true);
+		
+		
+		
+		JLabel portLabel = new JLabel("Port:");
+		portLabel.setBounds(10, 20, 35, 30);
+		settingsPanel.add(portLabel);
+		JFormattedTextField portTF = new JFormattedTextField(integerFormatter);
+		portTF.setText(""+port);
+		portTF.setBounds(55 ,20, 80, 30);
+		portTF.addPropertyChangeListener(propertyChange ->{
+			String text = portTF.getValue().toString();
+			text = text.replaceAll("\\s", "");
+			port = Integer.parseInt((text));
+		});
+		settingsPanel.add(portTF);
+		
+		
+		houseLabel = new JLabel("House Status: " + (holonObjectConnected?"Connected":"Not selected"));
+		houseLabel.setBounds(10, 90, 220, 30);
+		settingsPanel.add(houseLabel);
+		
+		JButton selectRoom1Button = new JButton("Select");
+		selectRoom1Button.setBounds(230,95, 90, 20);
+		selectRoom1Button.addActionListener(actionEvent -> this.selectHolonObject());
+		settingsPanel.add(selectRoom1Button);
+		
+		
+		JButton connectButton = new JButton("Start Server");
+		connectButton.setBounds(100 ,175, 200, 50);
+		connectButton.addActionListener(actionEvent -> connect());
+		settingsPanel.add(connectButton);
+
+		return settingsPanel;
+	}
+
+
+
+
+
+
+
+
+
+
+
+	private void connect() {
+		if(!holonObjectConnected) {
+			console.println("Select a HolonObject");
+			return;
+		}
+		try {
+			if(server != null) {
+				server.stopServer();
+			}
+			console.println("Start Server on Port:" + port);
+			server = new Server(port, console, observed, control);
+		}catch(BindException e){
+			console.println(e.getMessage());
+		}
+		catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+	
+
+	@Override
+	public JPanel getAlgorithmPanel() {
+		return content;
+	}
+
+	@Override
+	public void setController(Control control) {
+		this.control = control;
+	}
+
+	
+		private void selectHolonObject() {
+			List<HolonObject> holonObjectList = new ArrayList<HolonObject>();
+			addObjectToList(control.getModel().getObjectsOnCanvas(),holonObjectList);
+			Object[] possibilities = holonObjectList.stream().map(aCps -> new Handle<HolonObject>(aCps)).toArray();
+			@SuppressWarnings("unchecked")
+			Handle<HolonObject> selected = (Handle<HolonObject>) JOptionPane.showInputDialog(content, "Select HolonObject:", "HolonObject?",  JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
+			if(selected != null) {
+				console.println("Selected: " + selected);
+				observed = selected.object;
+				holonObjectConnected = true;
+				updateSelectionLabel();
+			}
+		}
+		
+		private void updateSelectionLabel() {
+			houseLabel.setText("House Status: " + (holonObjectConnected?"Connected":"Not selected"));
+		}
+
+
+
+
+
+		private void addObjectToList(List<AbstractCpsObject> listToSearch, List<HolonObject> listToAdd){
+			for (AbstractCpsObject aCps : listToSearch) {
+				if (aCps instanceof HolonObject) listToAdd.add((HolonObject) aCps);
+				else if(aCps instanceof CpsUpperNode) {
+					addObjectToList(((CpsUpperNode)aCps).getNodes(),listToAdd);
+				}
+			}
+		}
+		
+		
+		private   class  Handle<T>{
+			public T object;
+			Handle(T object){
+				this.object = object;
+			}
+			public String toString() {
+				return object.toString();
+			}
+		}
+	
+}

+ 94 - 58
src/Connection/ConnectPhysical.java → src/connection/ConnectPhysical.java

@@ -1,4 +1,4 @@
-package Connection;
+package connection;
 import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Dimension;
@@ -15,6 +15,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 import javax.swing.BorderFactory;
 import javax.swing.ImageIcon;
@@ -33,8 +34,10 @@ import javax.swing.text.NumberFormatter;
 import api.Algorithm;
 import classes.AbstractCpsObject;
 import classes.CpsUpperNode;
+import classes.HolonElement;
 import classes.HolonObject;
 import ui.controller.Control;
+import ui.view.Console;
 /**
  * Easy Connection via Http Request. Repeat Request with a delay.
  * 
@@ -50,10 +53,10 @@ public class ConnectPhysical implements Algorithm{
 	
 	//Gui
 	private JPanel content = new JPanel();
-	private JTextArea textArea;
-	JLabel rotorLabel;
-	JLabel room1Label;
-	JLabel room2Label;
+	private Console console;
+
+	private JLabel rotorLabel;
+	private JLabel houseLabel;
 	
 	//
 	Future<?> future;
@@ -80,8 +83,18 @@ public class ConnectPhysical implements Algorithm{
 	
 	//Object to look at
 	PhysicalLinkWrapper rotor = new PhysicalLinkWrapper(null, HolonObjectStatus.NotSelected, "/rotor/");
-	PhysicalLinkWrapper room1 = new PhysicalLinkWrapper(null, HolonObjectStatus.NotSelected, "/room1/");
-	PhysicalLinkWrapper room2 = new PhysicalLinkWrapper(null, HolonObjectStatus.NotSelected, "/room2/");
+	
+	//Because House is special
+	PhysicalLinkWrapper house = new PhysicalLinkWrapper(null, HolonObjectStatus.NotSelected, "notUsed");
+	String room1Address = "/room1/";
+	String room2Address = "/room2/";
+	//the keywords are for the sepreation in 2 rooms
+	String room1Keyword = "room1";
+	String room2Keyword = "room2";
+
+
+
+
 
 
 
@@ -109,10 +122,8 @@ public class ConnectPhysical implements Algorithm{
 	
 	public ConnectPhysical() {
 		content.setLayout(new BorderLayout());
-		
-		textArea = new JTextArea();
-		textArea.setEditable(false);
-		JScrollPane scrollPane = new JScrollPane(textArea);
+		console = new Console();
+		JScrollPane scrollPane = new JScrollPane(console);
 		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
 				createOptionPanel() , scrollPane);
 		splitPane.setResizeWeight(0.0);
@@ -135,12 +146,12 @@ public class ConnectPhysical implements Algorithm{
 		
 		
 		JLabel lessInformationLabel = new JLabel("Less information in Console:");
-		lessInformationLabel.setBounds(200, 150, 200, 20);
+		lessInformationLabel.setBounds(200, 180, 200, 20);
 		parameterPanel.add(lessInformationLabel);	
 		
 		JCheckBox lessInformationCheckBox = new JCheckBox();
 		lessInformationCheckBox.setSelected(false);
-		lessInformationCheckBox.setBounds(400, 150, 25, 20);
+		lessInformationCheckBox.setBounds(400, 180, 25, 20);
 		lessInformationCheckBox.addActionListener(actionEvent -> {
 			lessInformation = lessInformationCheckBox.isSelected();
 		});
@@ -148,15 +159,15 @@ public class ConnectPhysical implements Algorithm{
 		
 		
 		JLabel delayLabel = new JLabel("Delay:");
-		delayLabel.setBounds(200, 180, 50, 20);
+		delayLabel.setBounds(200, 210, 50, 20);
 		parameterPanel.add(delayLabel);	
 		
 		JLabel delayUnitLabel = new JLabel("[ms]");
-		delayUnitLabel.setBounds(300, 180, 50, 20);
+		delayUnitLabel.setBounds(300, 210, 50, 20);
 		parameterPanel.add(delayUnitLabel);	
 		
 		warningLabel = new JLabel(stringToHtml(stringWithColor("You need to Stop and Run again to affect delay change.", "red")));
-		warningLabel.setBounds(200, 210, 400, 20);
+		warningLabel.setBounds(200, 240, 400, 20);
 		warningLabel.setVisible(false);
 		parameterPanel.add(warningLabel);	
 		
@@ -175,11 +186,11 @@ public class ConnectPhysical implements Algorithm{
 		delayTextField.addPropertyChangeListener(actionEvent -> {
 			delay = Integer.parseInt(delayTextField.getValue().toString());
 			if(future != null && !future.isCancelled()) {
-				println("You need to Stop and Run again to affect this change.");
+				console.println("You need to Stop and Run again to affect this change.");
 				warningLabel.setVisible(true);
 			}
 		});
-		delayTextField.setBounds(250, 180, 50, 20);
+		delayTextField.setBounds(250, 210, 50, 20);
 		parameterPanel.add(delayTextField);
 
 		
@@ -188,13 +199,18 @@ public class ConnectPhysical implements Algorithm{
 		rotorLabel.setBounds(200, 60, 220, 30);
 		parameterPanel.add(rotorLabel);	
 		
-		room1Label = new JLabel(stringToHtml("Room1 Status: " + statusToString(room1.status)));
-		room1Label.setBounds(200, 90, 220, 30);
-		parameterPanel.add(room1Label);
+		houseLabel = new JLabel(stringToHtml("House Status: " + statusToString(house.status)));
+		houseLabel.setBounds(200, 90, 220, 30);
+		parameterPanel.add(houseLabel);
+		
+		JLabel keywordsLabel = new JLabel("Room Seperation Keywords: " + room1Keyword + " " + room2Keyword);
+		keywordsLabel.setBounds(200, 120, 320, 30);
+		parameterPanel.add(keywordsLabel);
+		
+		JLabel keywordsHintLabel = new JLabel("HolonElements with a name that contains the Keyword count.");
+		keywordsHintLabel.setBounds(200, 135, 450, 30);
+		parameterPanel.add(keywordsHintLabel);
 		
-		room2Label = new JLabel(stringToHtml("Room2 Status: " + statusToString(room2.status)));
-		room2Label.setBounds(200, 120, 220, 30);
-		parameterPanel.add(room2Label);
 		
 		JButton selectRotorButton = new JButton("Select");
 		selectRotorButton.setBounds(420,65, 90, 20);
@@ -203,13 +219,10 @@ public class ConnectPhysical implements Algorithm{
 		
 		JButton selectRoom1Button = new JButton("Select");
 		selectRoom1Button.setBounds(420,95, 90, 20);
-		selectRoom1Button.addActionListener(actionEvent -> this.selectGroupNode(room1));
+		selectRoom1Button.addActionListener(actionEvent -> this.selectGroupNode(house));
 		parameterPanel.add(selectRoom1Button);
 		
-		JButton selectRoom2Button = new JButton("Select");
-		selectRoom2Button.setBounds(420,125, 90, 20);
-		selectRoom2Button.addActionListener(actionEvent -> this.selectGroupNode(room2));
-		parameterPanel.add(selectRoom2Button);
+		
 		
 		return parameterPanel;
 	}
@@ -239,8 +252,7 @@ public class ConnectPhysical implements Algorithm{
 
 	private void  updateStatusLabels() {
 		rotorLabel.setText(stringToHtml("Rotor Status: " + statusToString(rotor.status)));
-		room1Label.setText(stringToHtml("Room1 Status: " + statusToString(room1.status)));
-		room2Label.setText(stringToHtml("Room2 Status: " + statusToString(room2.status)));
+		houseLabel.setText(stringToHtml("House Status: " + statusToString(house.status)));
 	}
 	
 	
@@ -248,7 +260,7 @@ public class ConnectPhysical implements Algorithm{
 	private Component createButtonPanel() {
 		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 		JButton clearButton =  new JButton("Clear Console");
-		clearButton.addActionListener(actionEvent -> clear());
+		clearButton.addActionListener(actionEvent -> console.clear());
 		buttonPanel.add(clearButton);
 		JButton stopButton =  new JButton("Stop");
 		stopButton.addActionListener(actionEvent -> stop());
@@ -263,38 +275,38 @@ public class ConnectPhysical implements Algorithm{
 	private void stop() {
 		if(future!= null) {
 			if(future.isCancelled()) {
-				println("Is cancelled.");
+				console.println("Is cancelled.");
 			}
 			else {
 				future.cancel(true);
-				println("Stopped sending Requests on localhost:2019 ...");
+				console.println("Stopped sending Requests on localhost:2019 ...");
 			}
 		}
 		else {
-			println("Not started jet.");
+			console.println("Not started jet.");
 		}
 	}
 
 	private void initSchedule() {
 		if(future != null && !future.isCancelled()) {
-			println("Is running.");
+			console.println("Is running.");
 			return;
 		}
 		warningLabel.setVisible(false);
-		println("Starting sending Requests on localhost:2019");
+		console.println("Starting sending Requests on localhost:2019");
 		final ScheduledExecutorService 	executorService = Executors.newSingleThreadScheduledExecutor();
 		final Runnable beeper = new Runnable() {
 				//RepeatedMethod
 		       	public void run() {
-		       		if(lessInformation)print(".");
+		       		if(lessInformation)console.print(".");
 		       		checkWrapper(rotor);
-		       		checkWrapper(room1);
-		       		checkWrapper(room2);
+		       		checkWrapperHouseSpecial(house);
 		       	}
 
+
 				private void checkWrapper(PhysicalLinkWrapper wrapper) {
 					if(wrapper.status == HolonObjectStatus.Connected) checkConnected(wrapper);
-		       		else if(!lessInformation)println(wrapper.postAddress +" is not connected.");
+		       		else if(!lessInformation)console.println(wrapper.postAddress +" is not connected.");
 				}
 
 				private void checkConnected(PhysicalLinkWrapper wrapper) {
@@ -305,19 +317,19 @@ public class ConnectPhysical implements Algorithm{
 					}
 					if(wrapper.hObject.getNumberOfElements() > 0) {
 						int value = Math.round(((float)wrapper.hObject.getNumberOfActiveElements()/(float) wrapper.hObject.getNumberOfElements())*(float) 100);					
-						sendRequest(wrapper, value);
+						sendRequest(wrapper.postAddress, value);
 						
 					}else {
-						sendRequest(wrapper, 0);
+						sendRequest(wrapper.postAddress, 0);
 					}
 						
 					
 						
 				}
 
-				private void sendRequest(PhysicalLinkWrapper wrapper, int value) {
-					if(!lessInformation)println("Send " + "http://localhost:2019" + wrapper.postAddress + value);
-					doHttpUrlConnectionAction("http://localhost:2019" + wrapper.postAddress + value);
+				private void sendRequest(String postAddress, int value) {
+					if(!lessInformation)console.println("Send " + "http://localhost:2019" + postAddress + value);
+					doHttpUrlConnectionAction("http://localhost:2019" + postAddress + value);
 				
 				}
 
@@ -344,13 +356,45 @@ public class ConnectPhysical implements Algorithm{
 				      connection.setReadTimeout(1000);
 				      connection.connect();
 				      } catch (MalformedURLException e) {
-				    	  println("MalformedURLException");
+				    	  console.println("MalformedURLException");
 				    	  e.printStackTrace();
 					} catch (IOException e) {
-						println("IOException: Connection refused");
+						console.println("IOException: Connection refused");
 						e.printStackTrace();
 					}
 				  }
+				  private void checkWrapperHouseSpecial(PhysicalLinkWrapper house) {
+					if(!(house.status == HolonObjectStatus.Connected)) {
+						if(!lessInformation)console.println("House" + " is not connected.");
+						return;
+					}
+			       	//House is Connected
+					if(house.hObject == null) {
+						house.status =  HolonObjectStatus.ObjectDeleted;
+						updateStatusLabels();
+						return;
+					}
+					//House exist
+					List<HolonElement> elementsOfRoom1 = house.hObject.getElements().stream().filter(ele -> ele.getEleName().contains(room1Keyword)).collect(Collectors.toList());
+					List<HolonElement> elementsOfRoom2 = house.hObject.getElements().stream().filter(ele -> ele.getEleName().contains(room2Keyword)).collect(Collectors.toList());
+					
+					if(elementsOfRoom1.isEmpty()){
+						sendRequest(room1Address, 0);
+					}
+					else{
+						int value = Math.round(((float)elementsOfRoom1.stream().filter(ele -> ele.isActive()).count()/(float) elementsOfRoom1.size())*(float) 100);					
+						sendRequest(room1Address, value);
+					}
+					if(elementsOfRoom2.isEmpty()){
+						sendRequest(room2Address, 0);
+					}
+					else{
+						int value = Math.round(((float)elementsOfRoom2.stream().filter(ele -> ele.isActive()).count()/(float) elementsOfRoom2.size())*(float) 100);					
+						sendRequest(room2Address, value);
+					}
+					
+					
+				  }
 		     };
 		future = executorService.scheduleAtFixedRate(beeper, 0, delay, TimeUnit.MILLISECONDS);
 	}
@@ -374,7 +418,7 @@ public class ConnectPhysical implements Algorithm{
 		@SuppressWarnings("unchecked")
 		Handle<HolonObject> selected = (Handle<HolonObject>) JOptionPane.showInputDialog(content, "Select HolonObject:", "HolonObject?",  JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
 		if(selected != null) {
-			println("Selected: " + selected);
+			console.println("Selected: " + selected);
 			wrapper.hObject = selected.object;
 			wrapper.status = HolonObjectStatus.Connected;
 			updateStatusLabels();
@@ -401,15 +445,7 @@ public class ConnectPhysical implements Algorithm{
 		this.control = control;
 	}
 	
-	private void clear() {
-		textArea.setText("");
-	}
-	private void print(String message) {
-		textArea.append(message);
-	}
-	private void println(String message) {
-		textArea.append(message  + "\n");
-	}
+
 	
 	
 

+ 280 - 0
src/connection/socket/Server.java

@@ -0,0 +1,280 @@
+package connection.socket;
+import java.net.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import classes.HolonElement;
+import classes.HolonObject;
+import ui.controller.Control;
+
+import java.io.*;
+
+public class Server implements Runnable{
+	private ServerSocket serverSocket;
+    private Socket clientSocket;
+    private DataOutputStream out;
+    private DataInputStream in;
+    private boolean stopped = false;		
+    private boolean connection = false;
+    private ui.view.Console console;
+    
+    
+    private HolonObject observed;
+    private HolonObjectModel model;
+	private Control control;
+    
+    public Server(int port, ui.view.Console console, HolonObject observed, Control control) throws IOException {
+    	this.observed = observed;
+    	this.console = console;
+    	this.control = control;
+    	//Bind Port
+    	serverSocket = new ServerSocket(port);
+        //Wait for Connection
+    	Thread serverThread = new Thread(this);
+        serverThread.start();
+            
+            
+    }
+ 
+    public void stopServer() throws IOException {
+    	stopped = true;
+        stopConnection();
+        if(serverSocket != null)serverSocket.close();
+		console.println("Server Closed");
+    }
+
+	private void stopConnection() throws IOException {
+		connection = false;
+		if(in != null)in.close();
+		if(out != null)out.close();
+		if(clientSocket != null)clientSocket.close();
+	}
+
+	@Override
+	public void run() {
+		while(!stopped) {
+			try {
+			//Wait for new Connection
+			console.println("Wait for Connection..");
+			clientSocket = serverSocket.accept();
+			console.println("Connection from " + clientSocket.getInetAddress() + ":" + clientSocket.getPort());
+			connection = true;
+			out = new DataOutputStream(clientSocket.getOutputStream());
+			in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
+			if(observed == null) stopConnection();	
+			createModel();
+			Thread updateThread = new Thread(new UpdateLoop());
+			updateThread.start();
+			//InputLoop
+			try {
+				inputLoop();
+			}catch(EOFException e){
+				console.println("Connection Closed");
+			}
+			stopConnection();	
+			}
+			catch(SocketException e){
+				//ServerSocket is closed
+				stopped = true;
+			}
+			catch (IOException e) {
+				e.printStackTrace();
+			}
+			connection = false;
+		}
+	}
+
+	private void inputLoop() throws IOException {
+		while (connection) {
+			//WaitForInput
+			byte inputByte = in.readByte();
+			//UpdateHoleg
+			if (inputByte == Command.SetAmount) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> SetAmount");
+				int index = in.readInt();
+				int amount = in.readInt();
+				observed.getElements().get(index).setAmount(amount);
+				
+			}else if (inputByte == Command.SetEnable) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> SetEnable");
+				int index = in.readInt();
+				boolean enabled = in.readBoolean();
+				observed.getElements().get(index).setActive(enabled);
+			}else if (inputByte == Command.IncreaseAmount) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> IncreaseAmount");
+				int index = in.readInt();
+				HolonElement ele = observed.getElements().get(index);
+				ele.setAmount(ele.getAmount()+1);
+				
+			}else if (inputByte == Command.DecreaseAmount) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> DecreaseAmount");
+				int index = in.readInt();
+				HolonElement ele = observed.getElements().get(index);
+				ele.setAmount(ele.getAmount()-1);
+				
+			} else{
+				console.println("Res: [" + inputByte + "] -> unknown");
+			}
+			control.calculateStateAndVisualForCurrentTimeStep();
+			control.updateCanvas();
+			control.getGui().triggerUpdateController(null);
+		}
+	}
+	
+	
+	private void createModel() {
+		model = new HolonObjectModel();
+		int index = 0;
+		for(HolonElement ele :observed.getElements()) {
+			model.add(ele, index++);
+		}
+	}
+	
+	
+	
+	
+	
+	public class UpdateLoop implements Runnable{
+		public UpdateLoop(){
+		}
+		@Override
+		public void run() {
+			sendUpdate();
+			while(!stopped && connection) {
+				if(checkForUpdates()){
+					sendUpdate();
+				}else {
+					waitOneSecond();	
+				}
+			}
+			
+		}
+
+		private void waitOneSecond() {
+			//wait one second
+			try {
+				TimeUnit.SECONDS.sleep(5);
+			} catch (InterruptedException e) {
+			}
+		}
+
+		private boolean checkForUpdates() {
+			//TODO Delete And CheckforUpdatesReal
+			//-->Test
+			waitOneSecond();
+			createModel();
+			//<--Test
+			return true;
+		}
+
+		private void sendUpdate() {
+			try {
+				if(observed == null) stopConnection();
+				console.println("Send: [" + Command.Update + "] -> Update");
+				out.writeByte(Command.Update);
+				out.writeInt(model.size());
+				for(HolonElementWrapper wrapper : model.getElements()) {
+					out.writeUTF(wrapper.name);
+					out.writeInt(wrapper.amount);
+					out.writeFloat(wrapper.energy);
+					out.writeBoolean(wrapper.enabled);
+				}
+				
+				
+				
+			} catch (IOException e) {
+				if(connection)console.println(e.getMessage());
+			}
+		}
+	}
+	
+	
+	public class Command {
+		//InputCommands
+		public static final int SetAmount = 10;
+		public static final int SetEnable = 11;
+		public static final int IncreaseAmount = 12;
+		public static final int DecreaseAmount = 13;
+		//UpdateResiveCommands
+		public static final int Update = 20;
+	}
+	
+	
+	
+	public class HolonObjectModel {
+		//Field
+		private List<HolonElementWrapper> elements;
+		private String holonObjectName;
+		
+		//constructor
+		public HolonObjectModel(){
+			elements = new ArrayList<HolonElementWrapper>();
+		}
+		
+		
+		public void add(HolonElement ele, int index ) {
+			String name = ele.getEleName();
+			int amount =ele.getAmount();
+			float energy =  ele.getEnergyPerElement();
+			boolean enabled = ele.isActive();
+			elements.add(new HolonElementWrapper(name, amount, energy, enabled, index));
+		}
+		
+		public int size() {
+			return elements.size();
+		}
+
+		//Getter/Setter
+		public List<HolonElementWrapper> getElements() {
+			return elements;
+		}
+		
+		public String getHolonObjectName() {
+			return holonObjectName;
+		}
+		public void setHolonObjectName(String holonObjectName) {
+			this.holonObjectName = holonObjectName;
+		}
+
+	}
+	public class HolonElementWrapper{
+		public int index;
+		public String name;
+		public int amount;
+		public float energy;
+		public boolean enabled;
+		public HolonElementWrapper(){
+			
+		}
+		public HolonElementWrapper(String name, int amount, float energy, boolean enabled, int index){
+			this.name = name;
+			this.amount = amount;
+			this.energy = energy;
+			this.enabled = enabled;
+		}
+		@Override
+		public boolean equals(Object obj) {
+			if (obj == this) {
+				return true;
+			}
+			if (!(obj instanceof HolonElementWrapper)) {
+			   return false;
+			}
+			HolonElementWrapper element = (HolonElementWrapper) obj;
+			return this.name == element.name &&
+				   this.amount == element.amount &&
+				   this.energy == element.energy &&
+				   this.enabled == element.enabled;
+		}
+		
+	}
+	
+	
+	
+	
+}

+ 2 - 2
src/ui/model/Model.java

@@ -41,8 +41,8 @@ public class Model {
     private int backgroundMode = 0;
     private int backgroundWidth = 0;
     private int backgroundHeight = 0;
-    private int canvasX = 1000;
-    private int canvasY = 1000;
+    private int canvasX = 3000;
+    private int canvasY = 3000;
     private int curIteration = 0;
     private LinkedList<Color> subNetColors = new LinkedList<>();
     // ID of the Selected Object

+ 27 - 0
src/ui/view/Console.java

@@ -0,0 +1,27 @@
+package ui.view;
+
+import javax.swing.JTextArea;
+import javax.swing.text.DefaultCaret;
+/**
+ * Little new swing object to print data to a console.
+ * @author tom
+ *
+ */
+public class Console extends JTextArea {
+	public Console() {
+		super();
+		this.setEditable(false);
+		DefaultCaret caret = (DefaultCaret)this.getCaret();
+		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
+	}
+	public void clear() {
+		this.setText("");
+	}
+	public void print(String message) {
+		this.append(message);
+
+	}
+	public void println(String message) {
+		this.append(message  + "\n");
+	}
+}

+ 2 - 1
src/ui/view/GUI.java

@@ -783,6 +783,7 @@ public class GUI implements CategoryListener {
 				}
 				controller.resetCategorys();
 			} catch (Exception e2) {
+				System.out.println(e2.getMessage());
 			}
 
 			tree.repaint();
@@ -3037,7 +3038,7 @@ public class GUI implements CategoryListener {
 				selectedValueBY, 2);
 	}
 
-	private void triggerUpdateController(AbstractCpsObject temp) {
+	public void triggerUpdateController(AbstractCpsObject temp) {
 		if (temp != null) {
 			updCon.paintProperties(temp);
 		}