package com.ociweb.oe.greenlightning.api;

import com.ociweb.gl.api.*;
import com.ociweb.oe.greenlightning.api.behaviors.EgressBehavior;
import com.ociweb.oe.greenlightning.api.behaviors.IngressBehavior;
import com.ociweb.oe.greenlightning.api.behaviors.TimeBehavior;

public class MQTTClient implements GreenApp {
	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 declareConfiguration(Builder 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, MQTTBridge.tlsPort, "MQTTClientTest",200) //default of 10 in flight
							.useTLS()
							.cleanSession(true)
							.keepAliveSeconds(10)
							.lastWill("last/will", false, MQTTQoS.atMostOnce, blobWriter -> {blobWriter.writeBoolean(true);});

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

	@Override
	public void declareBehavior(final GreenRuntime 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);
	}
}

@@ -260,16 +260,89 @@
 ehavior;
+%0Aimport com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler;
 %0A%0Apublic
@@ -706,16 +706,257 @@
 er) %7B%0A%09%09
+%0A%09%09//TODO: the base is wrong for a script since they do not match total...%0A%09%09%0A%09%09ScriptedNonThreadScheduler.debug = false;%0A%09%09%0A%09%09//without low latency this PCT does not work..%0A%09%09ScriptedNonThreadScheduler.lowLatencyEnforced = false;%0A%09%09%0A%09%09%0A%09%09//
 final St
@@ -985,16 +985,23 @@
 .0.0.1%22;
+ //1883
 %0A%09%09//fin
@@ -1125,16 +1125,104 @@
 rry Pi0%0A
+%09%09%0A%09%09final String brokerHost = %22test.mosquitto.org%22;%0A%09%09final int port = 1883;//8883;%0A%09%09%0A
 %09%09// Cre
@@ -1293,23 +1293,9 @@
 st, 
-MQTTBridge.tlsP
+p
 ort,
@@ -1350,16 +1350,23 @@
 %0A%09%09%09%09%09%09%09
+%0A%09%09%09//%09
 .useTLS(
@@ -1474,10 +1474,11 @@
 S.at
-Mo
+Lea
 stOn
@@ -1579,22 +1579,38 @@
 ate(
-3
+1
 00
+0
 ); 
+//once per second
 %0A%09%09%0A%09%09
-//
 buil
@@ -1629,16 +1629,20 @@
 lemetry(
+8099
 );%0A%09%7D%0A%0A%09

test code