Browse Source

House room seperation

Tom 5 years ago
parent
commit
c9fc94d692
1 changed files with 71 additions and 31 deletions
  1. 71 31
      src/Connection/ConnectPhysical.java

+ 71 - 31
src/Connection/ConnectPhysical.java

@@ -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,6 +34,7 @@ import javax.swing.text.NumberFormatter;
 import api.Algorithm;
 import classes.AbstractCpsObject;
 import classes.CpsUpperNode;
+import classes.HolonElement;
 import classes.HolonObject;
 import ui.controller.Control;
 /**
@@ -51,9 +53,8 @@ public class ConnectPhysical implements Algorithm{
 	//Gui
 	private JPanel content = new JPanel();
 	private JTextArea textArea;
-	JLabel rotorLabel;
-	JLabel room1Label;
-	JLabel room2Label;
+	private JLabel rotorLabel;
+	private JLabel houseLabel;
 	
 	//
 	Future<?> future;
@@ -80,8 +81,14 @@ 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";
 
 
 
@@ -135,12 +142,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 +155,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);	
 		
@@ -179,7 +186,7 @@ public class ConnectPhysical implements Algorithm{
 				warningLabel.setVisible(true);
 			}
 		});
-		delayTextField.setBounds(250, 180, 50, 20);
+		delayTextField.setBounds(250, 210, 50, 20);
 		parameterPanel.add(delayTextField);
 
 		
@@ -188,13 +195,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 +215,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 +248,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)));
 	}
 	
 	
@@ -288,10 +296,10 @@ public class ConnectPhysical implements Algorithm{
 		       	public void run() {
 		       		if(lessInformation)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.");
@@ -305,19 +313,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)println("Send " + "http://localhost:2019" + postAddress + value);
+					doHttpUrlConnectionAction("http://localhost:2019" + postAddress + value);
 				
 				}
 
@@ -351,6 +359,38 @@ public class ConnectPhysical implements Algorithm{
 						e.printStackTrace();
 					}
 				  }
+				  private void checkWrapperHouseSpecial(PhysicalLinkWrapper house) {
+					if(!(house.status == HolonObjectStatus.Connected)) {
+						if(!lessInformation)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);
 	}