Skip to content

Commit

Permalink
Incicio fuente
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres David Echeverri Jimenez committed Nov 24, 2017
1 parent 93905a5 commit fd50f16
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 101 deletions.
101 changes: 0 additions & 101 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions Conexion.py
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
45 changes: 45 additions & 0 deletions EchoBot.py
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()
33 changes: 33 additions & 0 deletions FbChat.py
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()
39 changes: 39 additions & 0 deletions ReadMsg.py
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)
26 changes: 26 additions & 0 deletions SendMsg.py
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])
15 changes: 15 additions & 0 deletions asincrono.py
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()
30 changes: 30 additions & 0 deletions procesar.py
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

0 comments on commit fd50f16

Please sign in to comment.