InfluxDB handling improvements

Signed-off-by: eantwuh <antal.wu-hen-chang@ericsson.com>
diff --git a/InfluxDB_Definitions.ttcn b/InfluxDB_Definitions.ttcn
index c83ab68..67a94c1 100644
--- a/InfluxDB_Definitions.ttcn
+++ b/InfluxDB_Definitions.ttcn
@@ -19,15 +19,20 @@
 	import from IPL4asp_Types all;
 	import from IPL4asp_PortType all;
 
+    modulepar boolean tsp_InfluxDB_debug := false;
 	modulepar integer tsp_InfluxDB_client_basePort := 9000;
+	modulepar float   tsp_InfluxDB_reportPeriod := 3.0;
 
 	type component InfluxDB_CT
 	{
       var InfluxDBContext v_InfluxDB_ctx := c_InfluxDBContext_empty;
 
-      var ASP_SendTo 			v_InfluxDB_sendAsp;
-      var charstring			v_InfluxDB_msgToSend_encoded;
-      var InfluxLineProtocol    v_InfluxDB_msgToSend;
+      var ASP_SendTo 			      v_InfluxDB_sendAsp;
+      var charstring			      v_InfluxDB_msgToSend_encoded;
+      var InfluxLineProtocol          v_InfluxDB_msgToSend;
+      var fcb_InfluxDB_reportHandler  v_InfluxDB_reportHandler := null;
+      var default                     v_InfluxDB_eventHandler;  
+      timer                           v_InfluxDB_reportTimer;
 
 	  port IPL4asp_PT INFLUXDB_PCO;
 	}
@@ -39,7 +44,8 @@
 		charstring remoteHost,
 		integer remotePort,
 		charstring localHost,
-		integer localPort
+		integer localPort,
+		float reportPeriod
 	}
 	with { extension "done" }
 
@@ -50,8 +56,11 @@
 		remoteHost := "127.0.0.1",
 		remotePort := 8089,
 		localHost := "127.0.0.1",
-		localPort := 9000
+		localPort := 9000,
+		reportPeriod := 3.0
 	}
+	
+	type function fcb_InfluxDB_reportHandler() runs on self;
 
 	type record InfluxLineProtocol
 	{
@@ -74,4 +83,10 @@
 	  charstring name,
 	  charstring val
 	}
+    
+    template InfluxAttrValue t_influxData(in charstring p_key, in charstring p_value) :=
+    {
+      name := p_key,
+      val := p_value
+    }
 }
diff --git a/InfluxDB_Functions.ttcn b/InfluxDB_Functions.ttcn
index 614a33f..ab27f86 100644
--- a/InfluxDB_Functions.ttcn
+++ b/InfluxDB_Functions.ttcn
@@ -20,36 +20,50 @@
 	import from IPL4asp_Types all;
 	import from IPL4asp_PortType all;
 
-	function f_InfluxDB_init() runs on InfluxDB_CT
+	function f_InfluxDB_init(in integer p_lgenIdx := 0) runs on InfluxDB_CT
 	{
-		log(%definitionId, " started");
-		var Result vl_result;
+		f_InlfuxDB_log_debug(log2str(%definitionId, " started"));
+		
+		f_InfluxDB_connect();
+		
+		v_InfluxDB_ctx.reportPeriod := tsp_InfluxDB_reportPeriod;
+		v_InfluxDB_ctx.localPort := tsp_InfluxDB_client_basePort + p_lgenIdx;
+		
+		v_InfluxDB_eventHandler := activate(as_InfluxDB_eventHandler());
+		
+		f_InlfuxDB_log_debug(log2str(%definitionId, " finished"));
+	}
+	
+	function f_InfluxDB_connect() runs on InfluxDB_CT
+	{
+        f_InlfuxDB_log_debug(log2str(%definitionId, " started"));
+        var Result vl_result;
 
-		log("Mapping started");
-		map(self:INFLUXDB_PCO,system:INFLUXDB_PCO);
+        f_InlfuxDB_log_debug(log2str("Mapping started"));
+        map(self:INFLUXDB_PCO,system:INFLUXDB_PCO);
 
-		log("InfluxDB context: ", v_InfluxDB_ctx);
+        f_InlfuxDB_log_debug(log2str("InfluxDB context: ", v_InfluxDB_ctx));
 
-		log("Setting up the listening socket");
-		vl_result := f_IPL4_listen(
-			INFLUXDB_PCO,
-			v_InfluxDB_ctx.localHost,
-			v_InfluxDB_ctx.localPort, {udp := {}},
-			{{reuseAddress := {enable := true}}}
-		);
-		v_InfluxDB_ctx.connId_listen := vl_result.connId;
+        f_InlfuxDB_log_debug(log2str("Setting up the listening socket"));
+        vl_result := f_IPL4_listen(
+            INFLUXDB_PCO,
+            v_InfluxDB_ctx.localHost,
+            v_InfluxDB_ctx.localPort, {udp := {}},
+            {{reuseAddress := {enable := true}}}
+        );
+        v_InfluxDB_ctx.connId_listen := vl_result.connId;
 
-		log("Connecting the socket to the remote");
-		vl_result := f_IPL4_connect(
-			INFLUXDB_PCO,
-			v_InfluxDB_ctx.remoteHost,
-			v_InfluxDB_ctx.remotePort,
-			v_InfluxDB_ctx.localHost,
-			v_InfluxDB_ctx.localPort,
-			-1,	{udp := {}}, {{reuseAddress := {enable := true}}}
-		);
-		v_InfluxDB_ctx.connId := vl_result.connId;
-		log(%definitionId, " finished");
+        f_InlfuxDB_log_debug(log2str("Connecting the socket to the remote"));
+        vl_result := f_IPL4_connect(
+            INFLUXDB_PCO,
+            v_InfluxDB_ctx.remoteHost,
+            v_InfluxDB_ctx.remotePort,
+            v_InfluxDB_ctx.localHost,
+            v_InfluxDB_ctx.localPort,
+            -1, {udp := {}}, {{reuseAddress := {enable := true}}}
+        );
+        v_InfluxDB_ctx.connId := vl_result.connId;
+        f_InlfuxDB_log_debug(log2str(%definitionId, " finished"));	  
 	}
 
 	function f_InfluxDB_send(in InfluxLineProtocol p_data) runs on InfluxDB_CT
@@ -63,29 +77,34 @@
 
 		v_InfluxDB_sendAsp.msg := char2oct(v_InfluxDB_msgToSend_encoded);
 
-		log("Influx: ", v_InfluxDB_msgToSend);
+		f_InlfuxDB_log_debug(log2str("Influx: ", v_InfluxDB_msgToSend));
 		INFLUXDB_PCO.send(v_InfluxDB_sendAsp);
 	}
 
 	function f_InfluxDB_encode(in InfluxLineProtocol p_prot, out charstring p_encoded)
 	{
-	  action("influx encoding: ", p_prot);
-	  p_encoded := p_prot.measurement;
+      f_InlfuxDB_log_debug(log2str("influx encoding: ", p_prot));
+      p_encoded := p_prot.measurement;
 
-	  if (sizeof(p_prot.tagSet)>0) { p_encoded := p_encoded & "," }
+      //if (sizeof(p_prot.tagSet)>0) { p_encoded := p_encoded & "," }
 
-	  for (var integer i:=0; i<sizeof(p_prot.tagSet); i:=i+1)
-	  {
-	    p_encoded := p_encoded & "," & p_prot.tagSet[i].name & "=" & p_prot.tagSet[i].val;
-	  }
-	  p_encoded := p_encoded & " "
-  	  for (var integer i:=0; i<sizeof(p_prot.fieldSet); i:=i+1)
-	  {
-	    p_encoded := p_encoded & p_prot.fieldSet[i].name & "=" & p_prot.fieldSet[i].val;
-	    if (i!=sizeof(p_prot.fieldSet)-1) { p_encoded := p_encoded & "," }
-	  }
-	  action("influx encoded: ", p_encoded);
+      for (var integer i:=0; i<sizeof(p_prot.tagSet); i:=i+1)
+      {
+        p_encoded := p_encoded & "," & p_prot.tagSet[i].name & "=" & p_prot.tagSet[i].val;
+      }
+      p_encoded := p_encoded & " "
+      for (var integer i:=0; i<sizeof(p_prot.fieldSet); i:=i+1)
+      {
+        p_encoded := p_encoded & p_prot.fieldSet[i].name & "=" & p_prot.fieldSet[i].val;
+        if (i!=sizeof(p_prot.fieldSet)-1) { p_encoded := p_encoded & "," }
+      }
+      f_InlfuxDB_log_debug(log2str("influx encoded: ", p_encoded));
 	}
+    
+    function f_InfluxDB_setMeasurement(inout InfluxLineProtocol p_pdu, in charstring p_measurement)
+    {
+      p_pdu.measurement := p_measurement;
+    }
 
 	function f_InfluxDB_addTag(inout InfluxLineProtocol p_pdu, in InfluxAttrValue p_tag)
 	{
@@ -96,4 +115,29 @@
 	{
 	  p_pdu.fieldSet[sizeof(p_pdu.fieldSet)] := p_field;
 	}
+	
+	function f_InfluxDB_scheduleNextReport()
+	runs on InfluxDB_CT
+	{
+	  v_InfluxDB_reportTimer.start(v_InfluxDB_ctx.reportPeriod);
+	}
+	
+	function f_InlfuxDB_log_debug(in charstring p_log)
+	{
+	  if (tsp_InfluxDB_debug){ log(p_log); }
+	}
+	
+  altstep as_InfluxDB_eventHandler()
+  runs on InfluxDB_CT
+  {
+    var integer vl_idx;
+    
+    [] v_InfluxDB_reportTimer.timeout
+    {
+      if (v_InfluxDB_reportHandler !=null) { v_InfluxDB_reportHandler.apply(); }
+
+      f_InfluxDB_scheduleNextReport();
+      repeat;
+    }
+  }
 }