From dad2ad7de1e849ce38108e27b5022f5f9908b695 Mon Sep 17 00:00:00 2001 From: afpinzon10 Date: Sat, 19 May 2018 10:37:13 -0500 Subject: [PATCH 1/2] bd alarmas --- EntidadVirtual/API_REST/insercion_alarmas.py | 12 +++++++++++- EntidadVirtual/API_REST/server.py | 5 +++-- EntidadVirtual/main.py | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/EntidadVirtual/API_REST/insercion_alarmas.py b/EntidadVirtual/API_REST/insercion_alarmas.py index f802a35..cc5d75f 100644 --- a/EntidadVirtual/API_REST/insercion_alarmas.py +++ b/EntidadVirtual/API_REST/insercion_alarmas.py @@ -1,5 +1,6 @@ from flask import Flask import paho.mqtt.client as paho +from datetime import date as d user = mqtt_connect.Client("C1") @@ -8,7 +9,16 @@ def on_message(user, data, message): alarmas = mongo.db.alarmas - if + 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) user.on_message = on_message diff --git a/EntidadVirtual/API_REST/server.py b/EntidadVirtual/API_REST/server.py index 5a41727..d8999e5 100644 --- a/EntidadVirtual/API_REST/server.py +++ b/EntidadVirtual/API_REST/server.py @@ -55,8 +55,9 @@ def insert_cerradura(): hora_inicio = request.json['hora_inicio'] hora_fin = request.json['hora_fin'] - nuevo_candado = "0;"+password+";"+posicion; - + nuevo_candado = "0;"+password+";"+posicion + user.on_message = on_message + user.loop_start() #hl.agregar_candado(hora_inicio, hora_fin, posicion) hl.agregar_candado(hora_inicio, hora_fin, posicion) client.publish(topico, nuevo_candado) diff --git a/EntidadVirtual/main.py b/EntidadVirtual/main.py index 3f3da04..a40ea8b 100644 --- a/EntidadVirtual/main.py +++ b/EntidadVirtual/main.py @@ -3,6 +3,7 @@ import time + def funt(): server.app.run() From b6a94f48e402b8a6ab7008c97a923160d10cf4e5 Mon Sep 17 00:00:00 2001 From: afpinzon10 Date: Sat, 19 May 2018 11:48:51 -0500 Subject: [PATCH 2/2] alarmas en tiempo real --- EntidadVirtual/API_REST/insercion_alarmas.py | 59 ++++++++++++++----- EntidadVirtual/API_REST/server.py | 6 +- .../NotificadorYale.py | 2 + 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/EntidadVirtual/API_REST/insercion_alarmas.py b/EntidadVirtual/API_REST/insercion_alarmas.py index cc5d75f..9789493 100644 --- a/EntidadVirtual/API_REST/insercion_alarmas.py +++ b/EntidadVirtual/API_REST/insercion_alarmas.py @@ -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() \ No newline at end of file +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() \ No newline at end of file diff --git a/EntidadVirtual/API_REST/server.py b/EntidadVirtual/API_REST/server.py index d8999e5..af86455 100644 --- a/EntidadVirtual/API_REST/server.py +++ b/EntidadVirtual/API_REST/server.py @@ -13,7 +13,7 @@ import json from os import environ as env from werkzeug.exceptions import HTTPException -import horarios.horariosLogic as hl +#import horarios.horariosLogic as hl from authlib.flask.client import OAuth from six.moves.urllib.parse import urlencode import requests @@ -59,8 +59,8 @@ def insert_cerradura(): user.on_message = on_message user.loop_start() #hl.agregar_candado(hora_inicio, hora_fin, posicion) - hl.agregar_candado(hora_inicio, hora_fin, posicion) - client.publish(topico, nuevo_candado) + #hl.agregar_candado(hora_inicio, hora_fin, posicion) + #client.publish(topico, nuevo_candado) return jsonify(nuevo_candado) diff --git a/EntidadVirtual/ServidorMensajeriaYCorreo/NotificadorYale.py b/EntidadVirtual/ServidorMensajeriaYCorreo/NotificadorYale.py index 12149f7..e22512f 100644 --- a/EntidadVirtual/ServidorMensajeriaYCorreo/NotificadorYale.py +++ b/EntidadVirtual/ServidorMensajeriaYCorreo/NotificadorYale.py @@ -93,3 +93,5 @@ def send_msg(mensaje, asunto): user.loop_start() time.sleep(1000) user.loop_stop() + +