-
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
Andres David Echeverri Jimenez
committed
Nov 24, 2017
1 parent
93905a5
commit fd50f16
Showing
8 changed files
with
224 additions
and
101 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
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 = "[email protected]" | ||
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() |
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 |
---|---|---|
@@ -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() |
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 |
---|---|---|
@@ -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) |
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 |
---|---|---|
@@ -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]) |
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 |
---|---|---|
@@ -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() |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Conexion | ||
import FbChat | ||
import sys | ||
|
||
TEMPERATURA_MINIMA = 21.94 | ||
TEMPERATURA_MAXIMA = 24.50 | ||
con = Conexion.Conexion() | ||
fbChat = FbChat.FbChat('[email protected]', '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 |