Skip to content

Latest commit

 

History

History
238 lines (162 loc) · 11.5 KB

DOCUMENTATION.md

File metadata and controls

238 lines (162 loc) · 11.5 KB

iGSE Energy Management System

Table of Contents

Requirements

  • Python 3.x or higher
  • For QR code detection, you will need to have a webcam connected to your computer and a webcam permission to access the webcam.

Get the Code

To get the code, open your terminal and go to the directory where you want to download the code. Then run the following command:

git clone https://github.com/Esh07/iGSE-Energy-Management-System.git

Environment Setup

NOTE: It is recommended to use a virtual environment to run the application. This will allow you to install the required packages in an isolated environment without affecting your system's global packages. You can use any virtual environment manager of your choice, such as virtualenv, venv, pipenv, etc. For this guide, we will be using the built-in venv module.

You can create a python virtual environment anywhere but the recommended way is to create it inside the project directory.

Go to the project directory

cd iGSE-Energy-Management-System

Create a virtual environment.

NOTE: You can replace <your-env-name> with any name you want.

Windows
 python -m venv <your-env-name>
MacOS / Linux
python3 -m venv <your-env-name>

Activate the environment.

Windows
<your-env-name>\Scripts\activate
MacOS / Linux
source <your-env-name>/bin/activate

Installation

Run the following command to install the necessary packages:

If you using macOS or Linux, you may need to use pip3 instead of pip; just to avoid dependency issues if you have both Python 2 and Python 3 installed on your system.

pip install -r requirements.txt

Database Setup

Inside the app.py file, you will find the following line of code that defines the database connection at line 28:

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://api_user:123456@localhost:3306/RestServiceInterface'

This line of code sets the database connection to a MySQL database with the following details:

  • api_user as the username
  • 123456 as the password
  • localhost:3306 as the host
  • RestServiceInterface as the database name

NOTE: Before running the application, you will need to create the database called RestServiceInterface or give a custom database name and update the app.py file (line 28) with your own database credentials.

Creating the Database

You can create the database using the following command: (it's one line query in MySQL)

Copy the following query and paste it in your MySQL Query Editor and run it.

CREATE DATABASE RestServiceInterface;

To run queries in MySQL in Windows, press ctrl + Enter to run the query. In MacOS, press command + Enter.

or you can do it by GUI. Execute query in MySQL workbench Execute query in MySQL workbench

Generating Tables from Models

NOTE: Make sure you're in the project directory and the virtual environment is activated. Otherwise, you will get an error.

To generate the necessary tables for the application, you will need to run the following command:

Only run this command once to initialize the database migrations
flask db init
Run the following commands to generate the migration files for the database tables
flask db migrate
Run the following command to apply the migration files to the database tables and generate the tables in the database
flask db upgrade

Running the Application

NOTE: If you set the FLASK_APP environment variable, other than app.py, you will need to configure the FLASK_APP environment variable to app.py before running the application.

For example, if you set the FLASK_APP environment variable to app.py, you will need to run the following command before running the application:

Macos / Linux
export FLASK_APP=app.py

To start the application, run the following command:

Flask run

You can also set the environment variable FLASK_APP and FLASK_DEBUG to run the application in debug mode:

Note: A variable is only valid for the current terminal session. If you close the terminal, the variable will be removed. And you will need to set the variable again.

MacOS / Linux
export FLASK_APP=app.py
export FLASK_DEBUG=1 # 1 to enable debug mode, 0 to disable debug mode
Windows (temporary session)
set FLASK_APP=app.py
set FLASK_DEBUG=1
Windows (permanent session)
This will set the environment variable permanently in the system and you will not need to set the variable again. 😄
setx FLASK_APP app.py
setx FLASK_DEBUG 1 # 1 to enable debug mode, 0 to disable debug mode

This will start the application on http://localhost:5000/

You should now be able to access the application in your browser by navigating to the specified URL. You can also access the application by entering the IP address of your machine on the network.

Extra settings

Changing the port

To change the port, you can set the FLASK_RUN_PORT environment variable:

export FLASK_RUN_PORT=8000

You can set the following environment variables to configure the application:

Environment Variable Description
FLASK_APP The name of the application file. Default: app.py
FLASK_ENV The environment to run the application in. Default: production
FLASK_DEBUG Enable or disable debug mode. Default: 0
FLASK_RUN_HOST The host to run the application on. Default: localhost
FLASK_RUN_PORT The port to run the application on. Default: 5000
FLASK_RUN_CERT The SSL certificate file to use to run the application. Default: None

API Documentation

The API documentation is available at http://localhost:5000/apidocs/

Endpoint Method Description
/ GET Home page
/home GET Home page
/register GET POST Register a new user
/login GET POST Login user
/reset-password GET POST Reset user password
/logout GET Logout user
/profile GET View user profile
/submit-meter-reading GET POST Submit a meter reading
view_latest_bill GET View the latest bill
/pay_bill/<int:bill_id> GET POST Pay a bill
top-up GET POST Top up account
/admin/register GET POST Register a new admin user
/admin/login GET POST Login admin user
/admin GET Admin dashboard
/admin/logout GET Logout admin user
/admin/set-tariffs GET POST Set the tariffs
admin/bills GET View all bills
/admin/bills/<int:bill_id> GET View a specific bill
/admin/meter-readings GET View all submitted meter readings
/admin/energy-statistics GET View energy statistics
/igse/propertycount GET API endpoint to get the count of properties
/igse/<property_type>/<num_bedrooms> GET API endpoint to get the average energy consumption for a property type and number of bedrooms
/check_email POST API endpoint to check if an email address is already registered (AJAX)
/check_evc_code POST API endpoint to check if an EVC code is valid (AJAX)

API Endpoints (Screenshot)

Endpoints screenshots