IoTool Save Data For Now

Introduction

The /api/iotool_save_data_for_now flow is designed to process HTTP GET requests for storing sensor data in a PostgreSQL database. It skillfully extracts user credentials from the header, employing basic authentication, and facilitates the management of sensor data, including the integration of sensor IDs provided in the GET request.

Flow Overview

The IoTool Save Data For Now workflow incorporates the following key operations:

  1. Reception of HTTP GET Requests: The flow begins by capturing an HTTP GET request, meticulously extracting user credentials embedded within the basic authentication header.

  2. Sensor and ID Registration: Efforts are made to register the sensor and its corresponding ID, as delineated in the GET request, into the database, ensuring each sensor is distinctly recognized.

  3. Looped Sensor ID Retrieval and Data Recording: The flow iteratively fetches the sensor’s ID using its name (as specified in the GET request) and commits the sensor data to the database.

Handling GET Requests

Purpose

While many of our examples utilize POST requests to transfer data, this section focuses on handling a GET request. Unlike POST requests, which carry data within the message body, GET requests encode data within the URL and headers. This example demonstrates how to extract user credentials from the request headers using basic authentication. Additional data elements are also retrieved from the headers. It is crucial to replace the placeholder username and password with the actual credentials corresponding to your PostgreSQL database. The extracted information is then employed to formulate a dynamic SQL query, enabling the retrieval of data entries from the specified table.

Configuration

Example of GET request to send:

{
    "info": {
        "_postman_id": "3c9c6706-93a9-4c4a-bce9-c48f65bc706a",
        "name": "New Collection",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
        "_exporter_id": "33058006"
    },
    "item": [
        {
            "name": "iotool_readings",
            "protocolProfileBehavior": {
                "disableBodyPruning": true
            },
            "request": {
                "auth": {
                    "type": "basic",
                    "basic": [
                        {
                            "key": "password",
                            "value": "password",
                            "type": "string"
                        },
                        {
                            "key": "username",
                            "value": "username",
                            "type": "string"
                        }
                    ]
                },
                "method": "GET",
                "header": [],
                "body": {
                    "mode": "urlencoded",
                    "urlencoded": [
                        {
                            "key": "un",
                            "value": "username",
                            "type": "text"
                        },
                        {
                            "key": "up",
                            "value": "password",
                            "type": "text"
                        },
                        {
                            "key": "db",
                            "value": "localhost",
                            "type": "text"
                        },
                        {
                            "key": "sensorcount",
                            "value": "1",
                            "type": "text"
                        },
                        {
                            "key": "db0",
                            "value": "senzor1",
                            "type": "text"
                        },
                        {
                            "key": "data0",
                            "value": ",1#,2#,3",
                            "type": "text"
                        }
                    ]
                },
                "url": {
                    "raw": "http://test.sandbox.engineering:1880/api/iotool_readings?sensor_id=123&min_datetimems=1609459200000&max_datetimems=1609545600000",
                    "protocol": "http",
                    "host": [
                        "test",
                        "sandbox",
                        "engineering"
                    ],
                    "port": "1880",
                    "path": [
                        "api",
                        "iotool_readings"
                    ],
                    "query": [
                        {
                            "key": "sensor_id",
                            "value": "123"
                        },
                        {
                            "key": "min_datetimems",
                            "value": "1609459200000"
                        },
                        {
                            "key": "max_datetimems",
                            "value": "1609545600000"
                        }
                    ]
                }
            },
            "response": []
        }
    ]
}

Sensor Addition and ID Assignment

This segment is tasked with the incorporation of sensor details, including its name and ID (as stipulated in the GET request), into the database, ensuring unique identification and tracking of each sensor.

Data Parsing and Structuring Function

This function is pivotal in parsing and structuring the incoming sensor data for database insertion. It addresses special numerical values (NaN, -Infinity, Infinity) by converting them into string representations to maintain database integrity. Following this, the data undergoes segmentation, formatting, and preparation for database entry, with each piece of data meticulously associated with a specific timestamp and sensor ID.

HTTP Response Confirmation

Upon the successful execution of data parsing, structuring, and insertion processes, the flow culminates in the transmission of an HTTP response, signifying the successful completion of the operation.

Overview

This documentation underscores the proficiency of Node-RED in handling HTTP GET requests, parsing critical credentials, managing sensor data, and interfacing with PostgreSQL databases. It exemplifies the dynamic and adaptable nature of Node-RED in streamlining data parsing, structuring, and database communication tasks.