From fd50f169967dc26a391af4bd36a978f8227a7ad3 Mon Sep 17 00:00:00 2001 From: Andres David Echeverri Jimenez Date: Fri, 24 Nov 2017 07:20:58 -0500 Subject: [PATCH] Incicio fuente --- .gitignore | 101 --------------------------------------------------- Conexion.py | 36 ++++++++++++++++++ EchoBot.py | 45 +++++++++++++++++++++++ FbChat.py | 33 +++++++++++++++++ ReadMsg.py | 39 ++++++++++++++++++++ SendMsg.py | 26 +++++++++++++ asincrono.py | 15 ++++++++ procesar.py | 30 +++++++++++++++ 8 files changed, 224 insertions(+), 101 deletions(-) create mode 100644 Conexion.py create mode 100644 EchoBot.py create mode 100644 FbChat.py create mode 100644 ReadMsg.py create mode 100644 SendMsg.py create mode 100644 asincrono.py create mode 100644 procesar.py diff --git a/.gitignore b/.gitignore index 697bc23..2122752 100644 --- a/.gitignore +++ b/.gitignore @@ -8,104 +8,3 @@ __pycache__/ .idea .gitignore - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -.hypothesis/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# pyenv -.python-version - -# celery beat schedule file -celerybeat-schedule - -# SageMath parsed files -*.sage.py - -# dotenv -.env - -# virtualenv -.venv -venv/ -ENV/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -Conexion.py -EchoBot.py -FbChat.py -ReadMsg.py -SendMsg.py -asincrono.py -procesar.py diff --git a/Conexion.py b/Conexion.py new file mode 100644 index 0000000..0699bb1 --- /dev/null +++ b/Conexion.py @@ -0,0 +1,36 @@ +#!/usr/bin/python + +import time +from time import sleep +import serial + + +class Conexion: + puerto_serie = None + + def __init__(self): + hola=1 + #self.conectar() + + def conectar(self): + self.puerto_serie = serial.Serial('/dev/ttyACM1', + baudrate=9600, + bytesize=serial.EIGHTBITS, + parity=serial.PARITY_NONE, + stopbits=serial.STOPBITS_ONE, + timeout=1, + xonxoff=0, + rtscts=0 + ) + + def close(self): + return 1 + #self.puerto_serie.close() + + def lectura(self): + #self.puerto_serie.setDTR(True) # indica que el canal esta listo para comunicarse + sleep(5) + #lectura = self.puerto_serie.readline().strip() + #print(time.strftime("%c")) + #lectura = float(lectura.replace(',', '.')) + return 25 diff --git a/EchoBot.py b/EchoBot.py new file mode 100644 index 0000000..b4fad0c --- /dev/null +++ b/EchoBot.py @@ -0,0 +1,45 @@ + +from fbchat import Client +import ReadMsg +import FbChat +from fbchat import log +from threading import Thread + + +class EchoBot(Client): + EMAIL_BOT = "sebas-g9412@hotmail.com" + PASS_BOT = "94122015402" + readMsg = None + fbChat = None + auxCode = 0 + auxAuthor = None + + def __init__(self, email, password): + super(EchoBot, self).__init__(email, password) + self.fbChat = FbChat.FbChat(email, password) + self.readMsg = ReadMsg.ReadMsg(self.fbChat) + + def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs): + self.markAsDelivered(author_id, thread_id) + self.markAsRead(author_id) + # imprimir el log para ver lo que esta pasando. + log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name)) + + # if self.auxCode != 0: + # self.fbChat.setIdChat(self.auxAuthor) + # self.readMsg.decifrarComando(self.auxCode, self.auxCode) + + if author_id != self.uid: + self.fbChat.setIdChat(author_id) + subproceso = Thread(target=self.subproceso, args=(message_object.text,)) + subproceso.start() + # self.auxCode = self.readMsg.decifrarComando() + # if self.auxCode != 0: + # self.auxAuthor = author_id + + def subproceso(self, msg): + self.readMsg.decifrarComando(msg) + + +client = EchoBot(EchoBot.EMAIL_BOT, EchoBot.PASS_BOT) +client.listen() diff --git a/FbChat.py b/FbChat.py new file mode 100644 index 0000000..f5e7055 --- /dev/null +++ b/FbChat.py @@ -0,0 +1,33 @@ +from fbchat import Client +from fbchat.models import * + + +class FbChat: + email = "" + password = "" + idChat = "" + client = None + + def __init__(self, email, password, idChat=None): + self.email = email + self.password = password + self.idChat = idChat + self.conectar() + + def conectar(self): + self.client = Client(self.email, self.password) + + def setEmail(self, email): + self.email = email + + def setIdChat(self, idChat): + self.idChat = idChat + + def setPassword(self, password): + self.password = password + + def sendMessageToPerson(self, msg): + self.client.send(Message(text=msg), thread_id=self.idChat, thread_type=ThreadType.USER) + + def close(self): + self.client.logout() diff --git a/ReadMsg.py b/ReadMsg.py new file mode 100644 index 0000000..597151d --- /dev/null +++ b/ReadMsg.py @@ -0,0 +1,39 @@ +import SendMsg +import Conexion + +class ReadMsg: + msg = "" + COMMAND_PEDIR_TEMPERATURA = 1 + sendMsg = None + stopMsg = 1 + con = None + + def __init__(self, fbChat): + self.sendMsg = SendMsg.sendMsg(fbChat) + + def setMessage(self, msg): + self.msg = msg + + def stopMsg(self): + self.stopMsg = 0 + + def decifrarComando(self, msg, code = None): # En este metodo deberias hacer lo de PLN + self.con = Conexion.Conexion() + if msg == "dame la temperatura": + fltTemperatura = self.con.lectura() + self.executeAction(SendMsg.sendMsg.ID_MSG_RESPONSE_TEMP, [fltTemperatura]) + self.con.close() + elif msg == "dame la temperatura en todo momento" or code == 1: + while self.stopMsg: + fltTemperatura = self.con.lectura() + self.executeAction(SendMsg.sendMsg.ID_MSG_RESPONSE_TEMP, [fltTemperatura]) + self.con.close() + return 1 + elif msg == "parar": + self.stopMsg = 0 + return 0 + + + def executeAction(self, comando, datos): + if comando == self.COMMAND_PEDIR_TEMPERATURA: + self.sendMsg.send(comando, datos) diff --git a/SendMsg.py b/SendMsg.py new file mode 100644 index 0000000..7cb9aa0 --- /dev/null +++ b/SendMsg.py @@ -0,0 +1,26 @@ +import FbChat + + +class sendMsg: + ID_MSG_RESPONSE_TEMP = 1 + ID_MSG_NO_ENTIENDO = 0 + # MSGS es un diccionario, aqui agreguen los mensajes que se desean responder + MSGS = { + ID_MSG_RESPONSE_TEMP: "La temperatura es: {} grados", + ID_MSG_NO_ENTIENDO: "No entiendo..." + } + fbChat = None + + def __init__(self, fbChat): + self.fbChat = fbChat + + def send(self, tipoMsg, datos): + msg = "" + if tipoMsg == self.ID_MSG_RESPONSE_TEMP: + msg = self.MSGS[tipoMsg].format(datos[0]) + # AquĆ­ anidar mas elif de lo que se quiera responder + + if msg != "": + self.fbChat.sendMessageToPerson(msg) + else: + self.fbChat.sendMessageToPerson(self.MSGS[self.ID_MSG_NO_ENTIENDO]) diff --git a/asincrono.py b/asincrono.py new file mode 100644 index 0000000..d80a46a --- /dev/null +++ b/asincrono.py @@ -0,0 +1,15 @@ +import asyncio +import datetime + +async def display_date(loop): + end_time = loop.time() + 5.0 + while True: + print(datetime.datetime.now()) + if (loop.time() + 1.0) >= end_time: + break + await asyncio.sleep(1) + +loop = asyncio.get_event_loop() +# Blocking call which returns when the display_date() coroutine is done +loop.run_until_complete(display_date(loop)) +loop.close() \ No newline at end of file diff --git a/procesar.py b/procesar.py new file mode 100644 index 0000000..65ce021 --- /dev/null +++ b/procesar.py @@ -0,0 +1,30 @@ +import Conexion +import FbChat +import sys + +TEMPERATURA_MINIMA = 21.94 +TEMPERATURA_MAXIMA = 24.50 +con = Conexion.Conexion() +fbChat = FbChat.FbChat('sebas-g9412@hotmail.com', '94122015402', '1116713387') + + +def enviar(msg): + fbChat.sendMessageToPerson(msg) + +while 1: + try: + ftLectura = con.lectura() + print(ftLectura) + msg = "" + if ftLectura <= TEMPERATURA_MINIMA: + msg = "La temperatura ha desendido hasta: ({}C)".format(str(ftLectura)) + elif ftLectura >= TEMPERATURA_MAXIMA: + msg = "La temperatura ha aumentado hasta: ({}C)".format(str(ftLectura)) + + if msg != "": + enviar(msg) + except: + fbChat.close() # Cierra la sesion + con.close() # Cierra la conexion al arduino + print("Unexpected error:", sys.exc_info()[0]) + raise