Commit 5a367002 by almagosi

Probably final commit

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