Git Product home page Git Product logo

door_brain's Introduction

door_brain

An API REST full to manage a mqtt communication between an escalable numbers of doors with RFID/NFC access.

requirements

  • postgresql
  • libpq-dev
  • python3
  • pip3

Setup

Init the data base:

First, we need to install postgresql. In linux:

sudo apt-get install libpq-dev postgresql

Create the Database, the user and his password

sudo su - postgres
createdb door-brain_DB
createuser lordmascachapas
psql door-brain_DB
ALTER USER lordmascachapas WITH PASSWORD 'admin';

'lordmascachapas' user with 'admin' password is defined in project settings, in door_brain/setting.py, in DATABASES property. Make sure you modify that property when using other credentials.

Then, install python requirements:

pip3 install -r requirements.txt

(Notice that libpq-dev and postgresql are needed to install psycopg2 with the python requirements)

Make the Django migrations:

python3 manage.py makemigrations
python3 manage.py migrate

Create an admin user:

python3 manage.py createsuperuser

Run the server:

python3 manage.py runserver [ip:port]

The http://127.0.0.1:8000/ path will display a swagger doc with the API endpoints. Probably most of this endpoints will be shown if logged as superuser only.

The http://127.0.0.1:8000/admin path will open an administration interface where login with super user credentials.

Usage

This API generates MQTT clients lo listen and save other clients (Doors) messages as logs.

Into the MQTT comunication

The MQTT protocol requires a broker, which will manage the clients messages. There are different ways to setup a MQTT broker. Here is an example with Mosquitto:

sudo apt-get update
sudo apt-get install mosquitto

CONGRATULATIONS !!!

Your MQTT broker is installed and operative. Now we need clients:

sudo apt-get install mosquitto-clients

MQTT clients need the broker ip to send and get messages. You can use 127.0.0.1 to test it in localhost.

To setup a subscriber type the next command:

mosquitto_sub -h 127.0.0.1 -t "some_topic"

A subscriber is a client which will wait for messages with "some_topic" as topic.

Now try to send a message from another command prompt. Publishers must send messages with the same topic:

mosquitto_pub -h 127.0.0.1 -t "some_topic" -m "some_message"

Your publisher sent a message and your subscriber has received that message

The Brain listens

Now try the API to start listen other MQTT clients. In the door_brain/settings.py file are the topics which the API will use to manage the doors

  • "logs": To listen the doors and save their messages as logs in a database
  • "manager": Which will be use to send message in broadcast
  • Each door have his own topic to send a message to the door brain.

To try the MQTT communication, is needed to tell the Brain to which brokers will send and listen messages. First, run the server with python3 manage.py runserver. Then, open the admin interface to create a new Broker instance with the ip 127.0.0.1, 1883 as port and 'logs listener' as duty. Also, you will need to create a door, MqttGroup, a management Broker and rule instances ( :D ).

Now start the MQTT communication with a GET request http://127.0.0.1:8000/logs/listenLogs.

To save a log, the message should be a jsonized data like this:

{
	"card_authorized": false,
	"door": "1789339278014108", 
	"date_time": "2013-01-29T12:34:56.000000Z", 
	"card_hash": "982341", 
	"reason": "card does not exists"
}

This is an example message, so make sure the 'door' field corresponds with the door id posted in the admin's page. 'date_time' must have the correct format as shown in the example.

Use mosquitto_pub -h 127.0.0.1 -t "logs" -m '{"card_authorized": false, "door": "1789339278014108", "date_time": "2013-01-29T12:34:56.000000Z", "card_hash": "982341", "reason": "card does not exists"}' command to publish the message. Make sure that a door with that identifier exists in the database.

A new log must be created in the database. To be sure, in the admin site must be the new Log instance with the published message. Also you can check it with the request http://127.0.0.1:8000/logs, which should return this JSON response:

[
    {
        "id": 1,
        "card_authorized": false,
        "reason": "card does not exists",
        "date_time": "2013-01-29T12:34:56Z",
        "card_hash": "982341",
        "door": "1789339278014108"
    },
]

The Brain talks

A management broker must be created at this point. If it doesn't, create a broker with the ip 127.0.0.1, 1883 as port and 'door manager' as duty

A message can be published with the GET request http://127.0.0.1:8000/manage/sendMessage/msg. The last extension msg will be the message to publish. Start a subscriber with mosquitto_sub -h 127.0.0.1 -t "manager" and make the request. The command console will display the message.

http://127.0.0.1:8000/manage/sendMessage/Hello

http://127.0.0.1:8000/manage/sendMessage/Dude

http://127.0.0.1:8000/manage/sendMessage/ooooh

linux:~$ mosquitto_sub -h 127.0.0.1 -t "manager"
Hello
Dude
ooooh

Other endpoints can be find on the swagger view.

door_brain's People

Contributors

lordmascachapas avatar dependabot[bot] avatar raular4322 avatar

Watchers

James Cloos avatar Néstor Javier Oubiña Arriaga avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.