Bläddra i källkod

Adds new class for MQTT Publish Packets

Andreas T. Meyer-Berg 5 år sedan
förälder
incheckning
77c180a9ec

+ 27 - 7
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java

@@ -17,6 +17,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensor;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatCollector;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensor;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets.MQTT_packet;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets.MQTTpublishPacket;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.AbstractEvent;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
 
@@ -259,6 +260,7 @@ public class MQTT_protocol implements Protocol {
 			 * Value which should be published
 			 */
 			String newValue = null;
+			boolean isBoolean = false;
 
 			/**
 			 * Check if FloatSensor
@@ -278,18 +280,24 @@ public class MQTT_protocol implements Protocol {
 				if (newTopic == null || new Random().nextBoolean()) {
 					newTopic = bSensor.getBSinfoName();
 					newValue = "" + bSensor.getBSval();
+					isBoolean = true;
 				}
 			}
 
 			if (newTopic != null) {
 				/**
-				 * Message to be published
+				 * Packet for publishing the new value
 				 */
-				String msg = "Topic:" + newTopic + ":" + newValue;
+				Packet  pubPacket = null;
+				if(isBoolean) {
+					pubPacket = new MQTTpublishPacket(timestep, port, broker, newTopic, Boolean.parseBoolean(newValue));
+				}else {
+					pubPacket = new MQTTpublishPacket(timestep, port, broker, newTopic, Float.parseFloat(newValue));
+				}
 				/**
 				 * Send Packet
 				 */
-				returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, port, broker, msg));
+				returnPackets.add(pubPacket);
 
 				/**
 				 * Delay to the broker
@@ -315,8 +323,15 @@ public class MQTT_protocol implements Protocol {
 						 */
 						long delayBrokerToSub = broker.getTransmissionDelayTo(p);
 						timestep += broker.getResponseTime();
-
-						returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
+						/**
+						 * Packet Broker -> Subscriber
+						 */
+						if(isBoolean) {
+							pubPacket = new MQTTpublishPacket(timestep, broker, p, newTopic, Boolean.parseBoolean(newValue));
+						}else {
+							pubPacket = new MQTTpublishPacket(timestep, broker, p, newTopic, Float.parseFloat(newValue));
+						}
+						returnPackets.add(pubPacket);
 						if (p.getStatus() != Port.CLOSED && delayBrokerToSub != Long.MAX_VALUE) {
 							returnPackets.add(
 									new MQTT_packet(MQTT_packet.PUBACK, timestep + p.getResponseTime(), p, broker));
@@ -368,8 +383,13 @@ public class MQTT_protocol implements Protocol {
 						 */
 						long delayBrokerToSub = broker.getTransmissionDelayTo(p);
 						timestep += broker.getResponseTime();
-
-						returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
+						
+						if(isBoolean) {
+							pubPacket = new MQTTpublishPacket(timestep, broker, p, newTopic, Boolean.parseBoolean(newValue));
+						}else {
+							pubPacket = new MQTTpublishPacket(timestep, broker, p, newTopic, Float.parseFloat(newValue));
+						}
+						returnPackets.add(pubPacket);
 						if (p.getStatus() != Port.CLOSED && delayBrokerToSub != Long.MAX_VALUE) {
 							returnPackets.add(
 									new MQTT_packet(MQTT_packet.PUBACK, timestep + p.getResponseTime(), p, broker));

+ 81 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/packets/MQTTpublishPacket.java

@@ -0,0 +1,81 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
+
+/**
+ * MQTT Publish Packet
+ * 
+ * 
+ * @author Andreas T. Meyer-Berg
+ */
+public class MQTTpublishPacket extends MQTT_packet {
+	/**
+	 * Topic of the Publish Packet
+	 */
+	private String topic = "";
+	/**
+	 * Value of the packet
+	 */
+	private float value = 0;
+	/**
+	 * True if boolean
+	 */
+	private boolean isBoolean = false;
+	
+	/**
+	 * MQTT Publish Packet
+	 * @param timestamp time it is send
+	 * @param source source port
+	 * @param destination destination port
+	 * @param topic topic of the message
+	 * @param value value of the message
+	 */
+	public MQTTpublishPacket(long timestamp, Port source, Port destination, String topic, float value) {
+		super(MQTT_packet.PUBLISH, timestamp, source, destination);
+		this.setTopic(topic);
+		this.setValue(value);
+		setBoolean(false);
+		this.message = topic + ":"+ value;
+	}
+	
+	/**
+	 * MQTT Publish Packet
+	 * @param timestamp time it is send
+	 * @param source source port
+	 * @param destination destination port
+	 * @param topic topic of the message
+	 * @param value value of the message
+	 */
+	public MQTTpublishPacket(long timestamp, Port source, Port destination, String topic, boolean value) {
+		super(MQTT_packet.PUBLISH, timestamp, source, destination);
+		this.setTopic(topic);
+		this.setValue(value ? 1 : 0);
+		setBoolean(true);
+		this.message = "Topic:"+topic + ":"+ value;
+	}
+
+	public boolean isBoolean() {
+		return isBoolean;
+	}
+
+	public void setBoolean(boolean isBoolean) {
+		this.isBoolean = isBoolean;
+	}
+
+	public float getValue() {
+		return value;
+	}
+
+	public void setValue(float value) {
+		this.value = value;
+	}
+
+	public String getTopic() {
+		return topic;
+	}
+
+	public void setTopic(String topic) {
+		this.topic = topic;
+	}
+
+}