Shelly Data Handling
Introduction
The /shelly/shelly
flow is built to handle HTTP GET requests
originating from the Shelly sensor. Its role is to act as an
intermediary, taking the sensor and user data, structuring it
appropriately, and then relaying it to a subsequent flow. This ensures
that the sensor data is persistently stored within a PostgreSQL
database.
Once the data is received and formatted by the /shelly/shelly
flow, it
is forwarded via an HTTP POST request to a distinct flow named
/android/iotool_lite_v3_savedata
. This secondary flow is responsible
for the final data handling and storage procedures.
Flow Overview
The Shelly workflow incorporates the following key operations:
-
Reception of HTTP GET request: The flow begins by capturing an HTTP GET request, which contains information like username and sensor data.
-
User search: Based on the incomming username flow extracts password for this user from postgreSQL database for later use.
-
HTTP response: Extracted password is sent back in form of reponse to incomming GET request.
-
Making POST request:
/android/iotool_lite_v3_savedata
is configured to accept POST request, last part of the flow is used to structure data into format that HTTP request node requires and sends it to specified endpoint.
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.
Handling of HTTP GET Requests
The function node within this flow is set up to construct dynamic SQL
queries. These queries facilitate the retrieval of data from the
registration database, with a primary focus on the pass' field, which
holds password information. In addition to this, the function
efficiently extracts the `username' from the `req.query
property,
enabling precise data selection based on the incoming GET request.
HTTP response
Upon successfull execution of postgreSQL query, password is formatted and sent back as response to initial request.
HTTP POST Request
This particular function node is adeptly configured to structure data
into the body of an HTTP POST request, which is crucial for initiating
interactions with a subsequent flow. It systematically assembles the
request content within an object named postdata
. This object is then
mapped to the msg.payload
property—a key step, as the msg.payload
is
what the HTTP request node utilizes to compose the actual body of the
request.
Additionally, the function designates the request URL as "http://localhost:1880/endpoint". This URL formulation is specifically chosen to facilitate seamless communication between different flows housed within the same Node-RED environment.