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 - replace 127.0.0.1 if using a different broker
	//
	//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) {
		final String brokerHost = "127.0.0.1";
		//final String brokerHost = "172.16.10.28"; // Nathan's PC
		//final String brokerHost = "thejoveexpress.local"; // Raspberry Pi0
		// Create a single mqtt client
		mqttConfig = builder.useMQTT(brokerHost, 1883, false, "MQTTClientTest",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";

		final MQTTQOS transQos = MQTTQOS.atLeastOnce;
		final MQTTQOS subscribeQos = MQTTQOS.atLeastOnce;

		// 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(transQos);
;
		// Subscribe to MQTT topic/ingress (created by mosquitto_pub example in comment above)
		runtime.bridgeSubscription(internalIngressTopic, externalIngressTopic, mqttConfig).setQoS(subscribeQos);
		// 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);
	}
}

@@ -1132,16 +1132,117 @@
 onds(10)
+%0A%09%09%09%09%09%09%09.will(false, MQTTQOS.atMostOnce, %22last/will%22, blobWriter -%3E %7BblobWriter.writeBoolean(true);%7D)
 ;%0A%0A%09%09// 
@@ -1289,16 +1289,18 @@
 00); %0A%09%09
+//
 builder.

add last will to test