package com.ociweb.oe.floglight.api;

import com.ociweb.gl.api.MQTTBridge;
import com.ociweb.gl.impl.MQTTQOS;
import com.ociweb.iot.maker.FogApp;
import com.ociweb.iot.maker.FogRuntime;
import com.ociweb.iot.maker.Hardware;
import com.ociweb.oe.floglight.api.behaviors.EgressBehavior;
import com.ociweb.oe.floglight.api.behaviors.IngressBehavior;
import com.ociweb.oe.floglight.api.behaviors.TimeBehavior;

public class MQTTClient implements FogApp {
	private MQTTBridge mqttConfig;
	
	//install mosquitto
	//
	//to monitor call >    mosquitto_sub -v -t '#' -h 127.0.0.1
	//to test call >       mosquitto_pub -h 127.0.0.1 -t 'external/topic/ingress' -m 'hello'

	@Override
	public void declareConnections(Hardware builder) {
		// Create a single mqtt client
		mqttConfig = builder.useMQTT(//"172.16.10.28", 1883, "NathansPC")
				                      "127.0.0.1", 1883, "my name",200) //default of 10 in flight
							.cleanSession(true)	
							.keepAliveSeconds(10);

		// Timer rate
		builder.setTimerPulseRate(300); 
		builder.enableTelemetry();
	}

	@Override
	public void declareBehavior(final FogRuntime runtime) {
		// The external/internal topic translation is not necessary.
		// The bridge calls may be made with one topic specified
		final String internalEgressTopic = "internal/topic/egress";
		final String externalEgressTopic = "external/topic/egress";
		final String internalIngressTopic = "internal/topic/ingress";
		final String externalIngressTopic = "external/topic/ingress";
		final String localTestTopic = "localtest";

		// Inject the timer that publishes topic/egress
		TimeBehavior internalEgressTopicProducer = new TimeBehavior(runtime, internalEgressTopic);
		runtime.addTimePulseListener(internalEgressTopicProducer);
		// Convert the internal topic/egress to external for mqtt
		runtime.bridgeTransmission(internalEgressTopic, externalEgressTopic, mqttConfig).setQoS(MQTTQOS.atLeastOnce);
;
		// Subscribe to MQTT topic/ingress (created by mosquitto_pub example in comment above)
		runtime.bridgeSubscription(internalIngressTopic, externalIngressTopic, mqttConfig).setQoS(MQTTQOS.atLeastOnce);
		// Listen to internal/topic/ingress and publish localtest
		IngressBehavior mqttBrokerListener = new IngressBehavior(runtime, localTestTopic);
		runtime.registerListener(mqttBrokerListener)
				.addSubscription(internalIngressTopic, mqttBrokerListener::receiveMqttMessage);

		// Inject the listener for "localtest"
		EgressBehavior doTheBusiness = new EgressBehavior();
		runtime.registerListener(doTheBusiness)
				.addSubscription(localTestTopic, doTheBusiness::receiveTestTopic);
	}
}

@@ -498,16 +498,64 @@
 osquitto
+ - replace 127.0.0.1 if using a different broker
 %0A%09//%0A%09//
@@ -774,158 +774,274 @@
 %09%09//
- Create a single mqtt client%0A%09%09mqttConfig = builder.useMQTT(//%22172.16.10.28%22, 1883, %22NathansPC%22)%0A%09%09%09%09                      %22127.0.0.1%22, 1883, %22my name
+final String brokerHost = %22127.0.0.1%22;%0A%09%09//final String brokerHost = %22172.16.10.28%22; // Nathan's PC%0A%09%09final String brokerHost = %22thejoveexpress.local%22; // Raspberry Pi0%0A%09%09// Create a single mqtt client%0A%09%09mqttConfig = builder.useMQTT(brokerHost, 1883, %22MQTTClientTest
 %22,20
@@ -1700,16 +1700,117 @@
 test%22;%0A%0A
+%09%09final MQTTQOS transQos = MQTTQOS.atLeastOnce;%0A%09%09final MQTTQOS subscribeQos = MQTTQOS.atLeastOnce;%0A%0A
 %09%09// Inj
@@ -2155,35 +2155,24 @@
 .setQoS(
-MQTTQOS.atLeastOnce
+transQos
 );%0A;%0A%09%09/
@@ -2349,35 +2349,28 @@
 .setQoS(
-MQTTQOS.atLeastOnce
+subscribeQos
 );%0A%09%09// 

If not using 127.0.0.1 and use thejoveexpress.local then This appears numerous times before connection is made “[MQTTClientToServerEncodeStage id:4] INFO com.ociweb.pronghorn.network.mqtt.MQTTClientToServerEncodeStage - no connection available” Subscription Behaviors are not called Transmission Behaviors are not seen by broker Eventually this floods the console “[MQTTClientToServerEncodeStage id:4] INFO com.ociweb.pronghorn.network.mqtt.MQTTClientToServerEncodeStage - does not have in flight capacity, B remaining limit 0”