Commit 5a367002 by almagosi

Probably final commit

parent aef7b635
...@@ -10,7 +10,7 @@ class Conexion: ...@@ -10,7 +10,7 @@ class Conexion:
with open(file="conexionConfig.json",mode='r') as f: with open(file="conexionConfig.json",mode='r') as f:
data = json.load(f) data = json.load(f)
self.__broker_address = data['ipbroker'] self.__broker_address = data['ipbroker']
self.__port = data['portbroker'] self.__port = int(data['portbroker'])
# Configuración del broker MQTT # Configuración del broker MQTT
#self.__broker_address = "192.168.48.245" # Cambia por la dirección de tu broker MQTT #self.__broker_address = "192.168.48.245" # Cambia por la dirección de tu broker MQTT
...@@ -38,9 +38,12 @@ class Conexion: ...@@ -38,9 +38,12 @@ class Conexion:
client.connect(self.__broker_address,self.__port) #connect to broker client.connect(self.__broker_address,self.__port) #connect to broker
client.loop_start() #start the loop client.loop_start() #start the loop
for tp in self.__topic: if(len(self.__topic) == 0):
client.subscribe(tp) print(f" Subscrito a {self.__topic}")
print(f" Subscrito a {tp}") else:
for tp in self.__topic:
client.subscribe(tp)
print(f" Subscrito a {tp}")
#self.__client = self.__connect_mqtt() #self.__client = self.__connect_mqtt()
#self.inicializar() #self.inicializar()
...@@ -76,6 +79,10 @@ class Conexion: ...@@ -76,6 +79,10 @@ class Conexion:
self.__client.disconnect() self.__client.disconnect()
print("Desconectado") print("Desconectado")
def desubscribir(self,topic):
self.__client.unsubscribe(topic)
print(f"Desubscrito topic : {topic}")
# Función para enviar mensajes MQTT al broker # Función para enviar mensajes MQTT al broker
def send_mqtt_message( topic,message): def send_mqtt_message( topic,message):
...@@ -113,3 +120,4 @@ def send_mqtt_message( topic,message): ...@@ -113,3 +120,4 @@ def send_mqtt_message( topic,message):
# client = connect_mqtt() # client = connect_mqtt()
# subscribe(client) # subscribe(client)
# client.loop_forever() # client.loop_forever()
{ {
"ipbroker" :"192.168.48.245", "ipbroker" :"192.168.0.19",
"portbroker" : "1883", "portbroker" : "1883",
"topicSubsRobot" : "A3-467/GrupoL/Robot", "topicSubsRobot" : ["A3-467/GrupoL/Robot","A3-467/GrupoL/Sinc"],
"topicSubsInterfaz" : ["A3-467/GrupoL/Interfaz","map"], "topicSubsInterfaz" : ["A3-467/GrupoL/Interfaz","map","A3-467/GrupoL/Sinc"],
"topicSendRobot" : "A3-467/GrupoL/Interfaz", "topicSendRobot" : "A3-467/GrupoL/Interfaz",
"topicSendInterfaz" : "A3-467/GrupoL/Robot", "topicSendInterfaz" : "A3-467/GrupoL/Robot",
"separadorMensaje" : ":", "separadorMensaje" : ":",
......
import conexion as cn import mensajeInterfaz as msg
import time import time
import casilla as cs
separadorTopic = "-" msgInterfaz = msg.MensageInterfaz()
mapa = msgInterfaz.getMapa()
print(f"Mapa recibido {mapa}")
mapa = [] print("Comprobando solicitud")
def leerMapa(msg : str): while(not msgInterfaz.pedidoSolicitado):
fila = 0 time.sleep(1)
columna = 0 print("Comprobando solicitud")
inicioPar = True
cas = ""
for c in msg:
cas = cas + c
if(not inicioPar):
mapa.append(cs.Casilla(fila,columna,int(cas)))
if(columna+1 == 5):
fila = fila+1
columna = 0
else:
columna = columna+1
cas = ""
inicioPar = not inicioPar
for casi in mapa:
print(casi)
posicion = msgInterfaz.getPosicion()
print(f"Posicion Recibida {posicion}")
pedido = ((4,5),(7,9))
lista = [] msgInterfaz.sendPedido(pedido)
print("Pedido enviado")
def on_message(client, userdata, message): \ No newline at end of file
print(f'Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
#if(message.topic == "map"):
# leerMapa(str(message.payload.decode("utf-8")))
#lista.append(f'{message.topic}{separadorTopic}{str(message.payload.decode("utf-8"))}')
topics = ["A3-467/GrupoL/Interfaz","map"]
conex = cn.Conexion(topics=topics,on_msg=on_message)
conex.publicar("A3-467/GrupoL/Robot","hola mundo")
conex.desconectar()
\ No newline at end of file
import conexion as cn
import time
mapa = "0202000105030705000200041109060110031000000200080101100110000106010701"
while(True):
cn.send_mqtt_message("map",mapa)
print("Enviado")
time.sleep(60)
\ No newline at end of file
import conexion as cn import mensajeRobot as msg
import time
lista = [] msgRobot = msg.MensageRobot()
def on_message(client, userdata, message): mapa = msgRobot.getMapa()
print(f'Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}') print(f"Mapa recibido {mapa}")
#lista.append(f'{message.topic}-{str(message.payload.decode("utf-8"))}')
topics = ["A3-467/GrupoL/Robot","map"] posicion= (0,6)
msgRobot.sendPosicion(posicion)
conex = cn.Conexion(topics=topics,on_msg=on_message) pedido = msgRobot.getPedido()
print(f"Pedido recibido {pedido}")
\ No newline at end of file
conex.desconectar()
\ No newline at end of file
...@@ -12,23 +12,31 @@ class MensageInterfaz: ...@@ -12,23 +12,31 @@ class MensageInterfaz:
def __init__(self) -> None: def __init__(self) -> None:
def filtrarMensage(topic,mensaje): def filtrarMensage(topic,mensaje):
if(topic == self.prefMsg['mapa'] and len(self.mapa) == 0): if(topic == self.prefMsg['mapa']):
self.mapa = mensaje self.mapa = mensaje
while(not self.sinc):
time.sleep(1)
self.__enviarMapa() self.__enviarMapa()
self.conex.desubscribir(topic)
else: else:
if(topic == self.topicSubs[-1]):
if(mensaje != str(self.__class__)):
self.sinc = True
else:
if(mensaje.split(self.sepMsg)[0] != self.prefMsg['pedido']):
self.pedidoSolicitado = True
if(mensaje.split(self.sepMsg)[0] != self.prefMsg['pedido']): if(mensaje.split(self.sepMsg)[0] != self.prefMsg['posicion']):
self.pedidoSolicitado = True self.posicion = list(mensaje.split(self.sepMsg)[1])
if(mensaje.split(self.sepMsg)[0] != self.prefMsg['posicion']):
self.posicion = list(mensaje.split(self.sepMsg)[1])
def on_message(client, userdata, message): def on_message(client, userdata, message):
#print(f'Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}') print(f'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
filtrarMensage(message.topic,str(message.payload.decode("utf-8"))) filtrarMensage(message.topic,str(message.payload.decode("utf-8")))
with open(file="conexionConfig.json",mode='r') as f: with open(file="conexionConfig.json",mode='r') as f:
...@@ -50,7 +58,15 @@ class MensageInterfaz: ...@@ -50,7 +58,15 @@ class MensageInterfaz:
self.mapa = "" self.mapa = ""
self.posicion : list self.posicion : list
self.pedidoSolicitado = False self.pedidoSolicitado = False
self.sinc = False
self.__sincronizacion()
def __sincronizacion(self):
while(not self.sinc):
self.conex.publicar(self.topicSubs[-1],str(self.__class__))
time.sleep(5)
self.conex.desubscribir(self.topicSubs[-1])
self.conex.publicar(self.topicSubs[-1],str(self.__class__))
def __enviarMapa(self): def __enviarMapa(self):
self.conex.publicar(self.topicSend,self.prefMsg['mapa'] + self.sepMsg + self.mapa) self.conex.publicar(self.topicSend,self.prefMsg['mapa'] + self.sepMsg + self.mapa)
...@@ -62,15 +78,15 @@ class MensageInterfaz: ...@@ -62,15 +78,15 @@ class MensageInterfaz:
return self.mapa return self.mapa
def solicitudPedido(self):
return self.pedidoSolicitado
def sendPedido(self,pedido): def sendPedido(self,pedido):
self.conex.publicar(self.topicSend,self.prefMsg['pedido'] + self.sepMsg + str(pedido)) self.conex.publicar(self.topicSend,self.prefMsg['pedido'] + self.sepMsg + str(pedido))
self.pedidoSolicitado = False
def getPosicion(self): def getPosicion(self):
while(self.posicion is None):
print("No hay posicion")
time.sleep(1)
return self.posicion return self.posicion
......
...@@ -12,8 +12,12 @@ class MensageRobot: ...@@ -12,8 +12,12 @@ class MensageRobot:
def __init__(self) -> None: def __init__(self) -> None:
def on_message(client, userdata, message): def on_message(client, userdata, message):
#print(f'Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}') print(f'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
self.mensaje = str(message.payload.decode("utf-8")) if(message.topic == self.topicSubs[-1]):
if(str(message.payload.decode("utf-8")) != str(self.__class__)):
self.sinc = True
else:
self.mensaje = str(message.payload.decode("utf-8"))
with open(file="conexionConfig.json",mode='r') as f: with open(file="conexionConfig.json",mode='r') as f:
data = json.load(f) data = json.load(f)
...@@ -32,6 +36,16 @@ class MensageRobot: ...@@ -32,6 +36,16 @@ class MensageRobot:
self.conex = cn.Conexion(self.topicSubs,on_msg=on_message) self.conex = cn.Conexion(self.topicSubs,on_msg=on_message)
self.mensaje = "" self.mensaje = ""
self.sinc = False
self.__sincronizacion()
def __sincronizacion(self):
while(not self.sinc):
self.conex.publicar(self.topicSubs[-1],str(self.__class__))
time.sleep(5)
self.conex.desubscribir(self.topicSubs[-1])
self.conex.publicar(self.topicSubs[-1],str(self.__class__))
def getMapa(self): def getMapa(self):
...@@ -46,6 +60,7 @@ class MensageRobot: ...@@ -46,6 +60,7 @@ class MensageRobot:
self.conex.publicar(self.topicSend,self.prefMsg['pedido'] + self.sepMsg) self.conex.publicar(self.topicSend,self.prefMsg['pedido'] + self.sepMsg)
while(self.mensaje.split(self.sepMsg)[0] != self.prefMsg['pedido']): while(self.mensaje.split(self.sepMsg)[0] != self.prefMsg['pedido']):
print(f"Esperando Pedido - {self.mensaje.split(self.sepMsg)[0],self.prefMsg['pedido']}")
time.sleep(1) time.sleep(1)
return self.mensaje.split(self.sepMsg)[1] return self.mensaje.split(self.sepMsg)[1]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment