-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dad2ad7
commit b6a94f4
Showing
3 changed files
with
49 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,54 @@ | ||
from flask import Flask | ||
from flask_pymongo import PyMongo | ||
import paho.mqtt.client as paho | ||
from datetime import date as d | ||
import time | ||
import json | ||
|
||
user = mqtt_connect.Client("C1") | ||
|
||
user.connect('localhost', port=8083) | ||
user.subscribe("conjunto1/residencia1/alerta") | ||
app = Flask(__name__) | ||
|
||
app.config['MONGO_DBNAME'] = 'restdb' | ||
app.config['MONGO_URI'] = 'mongodb://localhost:27017/restdb' | ||
|
||
mongo = PyMongo(app) | ||
|
||
user = paho.Client() | ||
|
||
|
||
|
||
def on_connect(client, userdata, flags, rc): | ||
print("CONNACK received with code %d." % (rc)) | ||
|
||
def on_message(user, data, message): | ||
alarmas = mongo.db.alarmas | ||
data = '{"fecha":"' + d.day + '/' + d.month + '/' + d.year + ', "cerradura":1 , "immueble": 1, "conjunto": 1, "tipo": ' | ||
if "P2" in message: | ||
data = data + '"Acceso no permitido" }' | ||
alarmas.insert(data) | ||
if "S1" in message: | ||
data = data + '"Intento de apertura sospechoso" }' | ||
alarmas.insert(data) | ||
if "M1" in message: | ||
data = data + '"Movimiento detectado" }' | ||
alarmas.insert(data) | ||
print(str(message.payload)) | ||
m = str(message.payload) | ||
with app.app_context(): | ||
alarmas = mongo.db.alarmas | ||
data = '{ "fecha":"' + d.today().day.__str__() + '/' + d.today().month.__str__() + '/' + d.today().year.__str__() + '", "cerradura":1 , "immueble": 1, "conjunto": 1, "tipo": ' | ||
if "P2" in m: | ||
data = data + '"Acceso no permitido" ' | ||
alarmas.insert(data) | ||
if "S1" in m: | ||
data = data + '"Intento de apertura sospechoso" ' | ||
alarmas.insert(data) | ||
if "M1" in m: | ||
data = data + '"Movimiento detectado" }' | ||
alarmas.insert(json.loads(data)) | ||
print(data) | ||
|
||
def on_subscribe(client, userdata,mid,granted_qos): | ||
print("subscribed: "+str(mid)+" "+str(granted_qos)) | ||
|
||
user.on_connect = on_connect | ||
user.on_subscribe = on_subscribe | ||
|
||
user.on_message = on_message | ||
user.loop_start() | ||
user.connect("broker.mqtt-dashboard.com", port=1883) | ||
print("suscribe 1") | ||
user.subscribe("conjunto1/residencia1/alerta") | ||
print("suscribe 2") | ||
user.loop_forever() | ||
|
||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,3 +93,5 @@ def send_msg(mensaje, asunto): | |
user.loop_start() | ||
time.sleep(1000) | ||
user.loop_stop() | ||
|
||
|