| /////////////////////////////////////////////////////////////////////////////// |
| // |
| // Copyright (c) 2000-2021 Ericsson Telecom AB |
| // |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v2.0 |
| // which accompanies this distribution, and is available at |
| // https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html |
| /////////////////////////////////////////////////////////////////////////////// |
| // File: InfluxDB_Functions.ttcn |
| // Description: |
| // Rev: R1B |
| // Prodnr: CNL 113 909 |
| // Updated: 2021-02-03 |
| // Contact: http://ttcn.ericsson.se |
| /////////////////////////////////////////////////////////////////////////////// |
| module InfluxDB_Functions { |
| |
| import from InfluxDB_Definitions all; |
| import from IPL4asp_Types all; |
| import from IPL4asp_PortType all; |
| |
| /////////////////////////////////////////////////////////// |
| // Function: f_IOT_LGen_behavior |
| // |
| // Purpose: |
| // The main initialization function for the <InfluxDB_CT> component type |
| // |
| // Parameters: |
| // p_lgenIdx - *in* *integer* - the index of the component instance |
| // |
| // Related Type: |
| // <InfluxDB_CT> |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_init(in integer p_lgenIdx := 0) runs on InfluxDB_CT |
| { |
| 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 |
| // |
| // Purpose: |
| // Setup the connection with the InfluxDB database |
| // |
| // Parameters: |
| // |
| // |
| // Related Type: |
| // <InfluxDB_CT> |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_connect() runs on InfluxDB_CT |
| { |
| f_InlfuxDB_log_debug(log2str(%definitionId, " started")); |
| var Result vl_result; |
| |
| f_InlfuxDB_log_debug(log2str("Mapping started")); |
| map(self:INFLUXDB_PCO,system:INFLUXDB_PCO); |
| |
| f_InlfuxDB_log_debug(log2str("InfluxDB context: ", v_InfluxDB_ctx)); |
| |
| 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; |
| |
| 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 |
| // |
| // Purpose: |
| // Sends a report to the InfluxDB database |
| // |
| // Parameters: |
| // p_data - *in* <InfluxLineProtocol> - Report to send |
| // |
| // Related Type: |
| // <InfluxDB_CT> |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_send(in InfluxLineProtocol p_data) runs on InfluxDB_CT |
| { |
| f_InfluxDB_encode(p_data, v_InfluxDB_msgToSend_encoded); |
| |
| v_InfluxDB_sendAsp.connId := v_InfluxDB_ctx.connId; |
| v_InfluxDB_sendAsp.remName := v_InfluxDB_ctx.remoteHost; |
| v_InfluxDB_sendAsp.remPort := v_InfluxDB_ctx.remotePort; |
| v_InfluxDB_sendAsp.proto := {udp := {}} |
| |
| v_InfluxDB_sendAsp.msg := char2oct(v_InfluxDB_msgToSend_encoded); |
| |
| f_InlfuxDB_log_debug(log2str("Influx: ", v_InfluxDB_msgToSend)); |
| INFLUXDB_PCO.send(v_InfluxDB_sendAsp); |
| } |
| |
| /////////////////////////////////////////////////////////// |
| // Function: f_InfluxDB_encode |
| // |
| // Purpose: |
| // Converts a report to charstring format |
| // |
| // Parameters: |
| // p_prot - *in* <InfluxLineProtocol>, Message to send |
| // p_encoded - *out* *charstring* - Output string |
| // |
| // Related Type: |
| // |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_encode(in InfluxLineProtocol p_prot, out charstring p_encoded) |
| { |
| f_InlfuxDB_log_debug(log2str("influx encoding: ", p_prot)); |
| p_encoded := p_prot.measurement; |
| |
| //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 & "," } |
| } |
| f_InlfuxDB_log_debug(log2str("influx encoded: ", p_encoded)); |
| } |
| |
| /////////////////////////////////////////////////////////// |
| // Function: f_InfluxDB_setMeasurement |
| // |
| // Purpose: |
| // Sets the measurement field of an InfluxDB report |
| // |
| // Parameters: |
| // p_pdu - *inout* <InfluxLineProtocol>, Influx message |
| // p_measurement - *in* *charstring* - measurement value |
| // |
| // Related Type: |
| // |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_setMeasurement(inout InfluxLineProtocol p_pdu, in charstring p_measurement) |
| { |
| p_pdu.measurement := p_measurement; |
| } |
| |
| /////////////////////////////////////////////////////////// |
| // Function: f_InfluxDB_addTag |
| // |
| // Purpose: |
| // Adds a tag to the tag-list of an InfluxDB message |
| // |
| // Parameters: |
| // p_pdu - *inout* <InfluxLineProtocol>, Influx message |
| // p_tag - *in* <InfluxAttrValue> - tag value to be added |
| // |
| // Related Type: |
| // |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_addTag(inout InfluxLineProtocol p_pdu, in InfluxAttrValue p_tag) |
| { |
| p_pdu.tagSet[sizeof(p_pdu.tagSet)] := p_tag; |
| } |
| |
| /////////////////////////////////////////////////////////// |
| // Function: f_InfluxDB_addField |
| // |
| // Purpose: |
| // Adds a field to the field-list of an InfluxDB message |
| // |
| // Parameters: |
| // p_pdu - *inout* <InfluxLineProtocol>, Influx message |
| // p_field - *in* <InfluxAttrValue> - field value to be added |
| // |
| // Related Type: |
| // |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_addField(inout InfluxLineProtocol p_pdu, in InfluxAttrValue p_field) |
| { |
| p_pdu.fieldSet[sizeof(p_pdu.fieldSet)] := p_field; |
| } |
| |
| /////////////////////////////////////////////////////////// |
| // Function: f_InfluxDB_scheduleNextReport |
| // |
| // Purpose: |
| // Sets the target time of the next report |
| // |
| // Parameters: |
| // |
| // Related Type: |
| // <InfluxDB_CT> |
| /////////////////////////////////////////////////////////// |
| function f_InfluxDB_scheduleNextReport() |
| runs on InfluxDB_CT |
| { |
| v_InfluxDB_reportTimer.start(v_InfluxDB_ctx.reportPeriod); |
| } |
| |
| /////////////////////////////////////////////////////////// |
| // Function: f_InlfuxDB_log_debug |
| // |
| // Purpose: |
| // If debug log is set, write the parameter to the log file |
| // |
| // Parameters: |
| // p_log - *in* *charstring* - string to log |
| // |
| // Related Type: |
| // |
| /////////////////////////////////////////////////////////// |
| function f_InlfuxDB_log_debug(in charstring p_log) |
| { |
| if (tsp_InfluxDB_debug){ log(p_log); } |
| } |
| |
| /////////////////////////////////////////////////////////// |
| // Function: as_InfluxDB_eventHandler |
| // |
| // Purpose: |
| // When report period expired, restarts the InfluxDB event handler and sets the time of the next report |
| // |
| // Parameters: |
| // |
| // Related Type: |
| // <InfluxDB_CT> |
| /////////////////////////////////////////////////////////// |
| 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; |
| } |
| } |
| } |