Skip to content

A robust Node.js backend service designed to ingest Modbus meter data via a REST API and store it in a PostgreSQL database. This service supports running as a Windows Service for continuous operation.

Notifications You must be signed in to change notification settings

SyntaxilitY/SyntaxilitY-IoT-nodejs-modebus-backend-service

Repository files navigation

IoT Node.js Modbus Backend Service

A robust Node.js backend service designed to ingest Modbus meter data via a REST API and store it in a PostgreSQL database. This service supports running as a Windows Service for continuous operation.

Features

  • REST API: Accepts Modbus meter data via HTTP POST requests.
  • Database Integration: Stores parsed meter readings in a PostgreSQL database.
  • Windows Service: Built-in support for installation as a background Windows Service (ModbusMeterService).
  • Data Processing: automatically calculates power factor and handles data type conversions.

Prerequisites

Before running this project, ensure you have the following installed:

Installation

  1. Clone the repository (or extract the project files):

    git clone <repository-url>
    cd IoT-nodejs-modebus-backend-service
  2. Install dependencies:

    npm install

Configuration

  1. Environment Variables: Create a .env file in the root directory if it doesn't exist, or configure your environment with the following variables. Defaults are provided in the code if not specified.

    PORT=5000
    DB_USER=postgres
    DB_HOST=localhost
    DB_NAME=Building_Automation_Management_System
    DB_PASSWORD=your_password
    DB_PORT=5432
  2. Database Setup: Ensure your PostgreSQL database exists and has the required schema. Run the following SQL to create the table:

    CREATE SCHEMA IF NOT EXISTS bms;
    
    CREATE TABLE IF NOT EXISTS bms.modbus_meter_data (
        id SERIAL PRIMARY KEY,
        meter_id VARCHAR(255),
        received_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        voltage_a NUMERIC,
        voltage_b NUMERIC,
        voltage_c NUMERIC,
        current_a NUMERIC,
        current_b NUMERIC,
        current_c NUMERIC,
        power_a NUMERIC,
        power_b NUMERIC,
        power_c NUMERIC,
        total_power NUMERIC,
        energy_import NUMERIC,
        energy_export NUMERIC,
        frequency NUMERIC,
        pf_a NUMERIC,
        pf_b NUMERIC,
        pf_c NUMERIC,
        pf_total NUMERIC,
        current_demand NUMERIC,
        max_demand NUMERIC,
        raw_payload JSONB
    );

Usage

Running Locally (Development)

To start the server in development mode with auto-reload:

npm run dev

The server will start on http://localhost:5000 (or your configured PORT).

Running as a Windows Service

To install and start the application as a Windows Service:

  1. Open a terminal as Administrator.
  2. Run the installation script:
    node service_install.js
  3. The service ModbusMeterService will be installed and started automatically.

To uninstall the service (if needed in the future), you would typically use a similar script using svc.uninstall().

API Documentation

Ingest Meter Data

  • URL: /api/modbus/meter-data
  • Method: POST
  • Content-Type: application/json

Request Body Example

The API expects a JSON payload with the following structure (keys map to specific database columns):

{
  "meter": "Meter_001",
  "Ua": 230.5,
  "Ub": 231.0,
  "Uc": 229.8,
  "Ia": 10.5,
  "Ib": 11.2,
  "Ic": 10.8,
  "Pa": 2400,
  "Pb": 2500,
  "Pc": 2450,
  "Pt": 7350,
  "PositiveEnergy": 15000.5,
  "NegativeEnergy": 50.0,
  "freq": 50.0,
  "PF_A": 95,
  "PF_B": 96,
  "PF_C": 94,
  "PF_T": 95,
  "CDt": 7000,
  "MDt": 8000
}

Response

  • Success (200 OK):
    {
      "success": true
    }

Project Structure

  • server.js: Main entry point of the application.
  • service_install.js: Script to install the app as a Windows Service.
  • controllers/: Contains logic for handling requests (e.g., modbusController.js).
  • routes/: Defines API endpoints (e.g., modbusRoutes.js).
  • daemon/: Directory where service logs and executables are stored after installation.

About

A robust Node.js backend service designed to ingest Modbus meter data via a REST API and store it in a PostgreSQL database. This service supports running as a Windows Service for continuous operation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published