Commit cbdbb574 by almagosi

Probably final commit

parents e9b6f98b d3a6ecda
...@@ -3,8 +3,10 @@ import json ...@@ -3,8 +3,10 @@ import json
class Conexion: class Conexion:
def on_message(client,userdata,message):
print(f'MENSAGE PERDIDO - Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
def __init__(self, topics,on_msg): def __init__(self, topics:list,on_msg:list):
with open(file="conexionConfig.json",mode='r') as f: with open(file="conexionConfig.json",mode='r') as f:
...@@ -19,6 +21,7 @@ class Conexion: ...@@ -19,6 +21,7 @@ class Conexion:
#self.__topic = ["A3-467/GrupoL","map"] #self.__topic = ["A3-467/GrupoL","map"]
self.__topic = topics self.__topic = topics
#def on_message(client, userdata, message): #def on_message(client, userdata, message):
# print("message received " ,str(message.payload.decode("utf-8"))) # print("message received " ,str(message.payload.decode("utf-8")))
...@@ -30,29 +33,30 @@ class Conexion: ...@@ -30,29 +33,30 @@ class Conexion:
#broker_address="192.168.0.19" #broker_address="192.168.0.19"
#broker_address="iot.eclipse.org" #broker_address="iot.eclipse.org"
print("creating new instance") print("creating new instance")
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
#client.on_message = on_message #client.on_message = on_message
#client.on_message=on_message #attach function to callback #client.on_message=on_message #attach function to callback
client.on_message = on_msg #client.on_message = on_msg
for tp,df in zip(topics,on_msg):
self.client.message_callback_add(tp,df)
print(f" Funcion {df} adjunta a {tp}")
print("connecting to broker") print("connecting to broker")
client.connect(self.__broker_address,self.__port) #connect to broker self.client.connect(self.__broker_address,self.__port) #connect to broker
client.loop_start() #start the loop
if(len(self.__topic) == 0): for tp in topics:
print(f" Subscrito a {self.__topic}") self.client.subscribe(tp)
else: print(f" Subscrito a {tp}")
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()
#self.__client.loop_start() #self.__client.loop_start()
self.client.on_message = self.on_message
self.client.loop_start() #start the loop
#self.subscribe(cliente=client,topic=self.__topic) #self.subscribe(cliente=client,topic=self.__topic)
self.__client = client
print("Inicializado") print("Inicializado")
#self.__listaPendientes = listaPendientes #self.__listaPendientes = listaPendientes
...@@ -62,10 +66,10 @@ class Conexion: ...@@ -62,10 +66,10 @@ class Conexion:
def publicar(self,topic,msg): def publicar(self,topic,msg):
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.connect(self.__broker_address, self.__port) #client.connect(self.__broker_address, self.__port)
result = client.publish(topic,msg) result = self.client.publish(topic,msg)
client.disconnect() #client.disconnect()
#result = self.__client.publish(self.__topic[0],msg) #result = self.__client.publish(self.__topic[0],msg)
# result: [0, 1] # result: [0, 1]
status = result[0] status = result[0]
...@@ -75,12 +79,12 @@ class Conexion: ...@@ -75,12 +79,12 @@ class Conexion:
print(f"Failed to send message to topic {topic}") print(f"Failed to send message to topic {topic}")
def desconectar(self): def desconectar(self):
self.__client.loop_stop() self.client.loop_stop()
self.__client.disconnect() self.client.disconnect()
print("Desconectado") print("Desconectado")
def desubscribir(self,topic): def desubscribir(self,topic):
self.__client.unsubscribe(topic) self.client.unsubscribe(topic)
print(f"Desubscrito topic : {topic}") print(f"Desubscrito topic : {topic}")
......
{ {
"ipbroker" :"192.168.0.19", "ipbroker" :"192.168.0.19",
"portbroker" : "1883", "portbroker" : "1883",
"topicSubsRobot" : ["A3-467/GrupoL/Robot","A3-467/GrupoL/Sinc"], "topicSubsRobot" : ["A3-467/GrupoL/Robot","A3-467/GrupoL/SincRobot"],
"topicSubsInterfaz" : ["A3-467/GrupoL/Interfaz","map","A3-467/GrupoL/Sinc"], "topicSubsInterfaz" : ["A3-467/GrupoL/Interfaz","map","A3-467/GrupoL/SincInterfaz"],
"topicSendRobot" : "A3-467/GrupoL/Interfaz", "topicSendRobot" : ["A3-467/GrupoL/Interfaz","A3-467/GrupoL/SincInterfaz"],
"topicSendInterfaz" : "A3-467/GrupoL/Robot", "topicSendInterfaz" : ["A3-467/GrupoL/Robot","A3-467/GrupoL/SincRobot"],
"separadorMensaje" : ":", "separadorMensaje" : ":",
"prefijoMensajes" : { "prefijoMensajes" : {
"mapa":"map", "mapa":"map",
"pedido":"ped", "pedido":"ped",
"posicion":"pos" "posicion":"pos",
"sinc" : "Sincronizar"
} }
} }
\ No newline at end of file
import mensajeInterfaz as msg import mensajeInterfaz as msg
import time import time
msgInterfaz = msg.MensageInterfaz() msgInterfaz = msg.MensajeInterfaz()
mapa = msgInterfaz.getMapa() mapa = msgInterfaz.getMapa()
print(f"Mapa recibido {mapa}") print(f"Mapa recibido {mapa}")
...@@ -13,7 +13,10 @@ while(not msgInterfaz.pedidoSolicitado): ...@@ -13,7 +13,10 @@ while(not msgInterfaz.pedidoSolicitado):
posicion = msgInterfaz.getPosicion() posicion = msgInterfaz.getPosicion()
print(f"Posicion Recibida {posicion}") print(f"Posicion Recibida {posicion}")
print(f'Posicion x = {posicion[0]} : y = {posicion[1]}')
pedido = ((4,5),(7,9)) pedido = ((4,5),(7,9))
msgInterfaz.sendPedido(pedido) msgInterfaz.sendPedido(pedido)
print("Pedido enviado") print("Pedido enviado")
\ No newline at end of file
time.sleep(60)
\ No newline at end of file
...@@ -6,4 +6,4 @@ mapa = "0202000105030705000200041109060110031000000200080101100110000106010701" ...@@ -6,4 +6,4 @@ mapa = "0202000105030705000200041109060110031000000200080101100110000106010701"
while(True): while(True):
cn.send_mqtt_message("map",mapa) cn.send_mqtt_message("map",mapa)
print("Enviado") print("Enviado")
time.sleep(60) time.sleep(30)
\ No newline at end of file \ No newline at end of file
...@@ -6,46 +6,55 @@ import json ...@@ -6,46 +6,55 @@ import json
class MensageInterfaz: class MensajeInterfaz:
def __init__(self) -> None: def __init__(self) -> None:
def filtrarMensage(topic,mensaje):
if(topic == self.prefMsg['mapa']):
self.mapa = mensaje
while(not self.sinc): def on_message_default(client,userdata,message):
time.sleep(1) print(f'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
mensaje = str(message.payload.decode("utf-8"))
if(mensaje.split(self.sepMsg)[0] == self.prefMsg['pedido']):
self.pedidoSolicitado = True
self.__enviarMapa() if(mensaje.split(self.sepMsg)[0] == self.prefMsg['posicion']):
self.conex.desubscribir(topic) mensaje = str(message.payload.decode("utf-8"))
sspos = mensaje.split(self.sepMsg)[1].split(",")
ssx = int(sspos[0].split("(")[1])
ssy = int(sspos[1].split(")")[0])
self.posicion = (ssx,ssy)
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['posicion']): def on_message_mapa(client,userdata,message):
self.posicion = list(mensaje.split(self.sepMsg)[1]) print(f'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
self.mapa = str(message.payload.decode("utf-8"))
while(not self.sinc):
time.sleep(2)
self.__enviarMapa()
self.conex.desubscribir(message.topic)
def on_message(client, userdata, message): def on_message_sinc(client,userdata,message):
print(f'Recive 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"))) self.sinc = True
#def on_message(client, userdata, message):
# 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: with open(file="conexionConfig.json",mode='r') as f:
data = json.load(f) data = json.load(f)
#self.topicSubs = ["A3-467/GrupoL/Robot"] #self.topicSubs = ["A3-467/GrupoL/Robot"]
self.topicSubs = data['topicSubsInterfaz'] self.topicSubs = data['topicSubsInterfaz']
print(f'Topics a subscribir {self.topicSubs}')
#self.topicSend = ["A3-467/GrupoL/Interfaz"] #self.topicSend = ["A3-467/GrupoL/Interfaz"]
self.topicSend = data['topicSendInterfaz'] self.topicSend = data['topicSendInterfaz']
print(f'Topics a enviar {self.topicSend}')
#self.prefMsg = {"mapa":'map', #self.prefMsg = {"mapa":'map',
# "pedido":'ped', # "pedido":'ped',
...@@ -53,8 +62,9 @@ class MensageInterfaz: ...@@ -53,8 +62,9 @@ class MensageInterfaz:
self.prefMsg = data['prefijoMensajes'] self.prefMsg = data['prefijoMensajes']
self.sepMsg = data['separadorMensaje'] self.sepMsg = data['separadorMensaje']
self.conex = cn.Conexion(self.topicSubs,on_msg=on_message) defs = [on_message_default,on_message_mapa,on_message_sinc]
self.conex = cn.Conexion(self.topicSubs,on_msg=defs)
self.mapa = "" self.mapa = ""
self.posicion : list self.posicion : list
self.pedidoSolicitado = False self.pedidoSolicitado = False
...@@ -63,13 +73,14 @@ class MensageInterfaz: ...@@ -63,13 +73,14 @@ class MensageInterfaz:
def __sincronizacion(self): def __sincronizacion(self):
while(not self.sinc): while(not self.sinc):
self.conex.publicar(self.topicSubs[-1],str(self.__class__)) self.conex.publicar(self.topicSend[-1],self.prefMsg['sinc'])
time.sleep(5) time.sleep(2)
print("SINCRONIZADO")
self.conex.desubscribir(self.topicSubs[-1]) self.conex.desubscribir(self.topicSubs[-1])
self.conex.publicar(self.topicSubs[-1],str(self.__class__)) self.conex.publicar(self.topicSend[-1],self.prefMsg['sinc'])
def __enviarMapa(self): def __enviarMapa(self):
self.conex.publicar(self.topicSend,self.prefMsg['mapa'] + self.sepMsg + self.mapa) self.conex.publicar(self.topicSend[0],str(self.prefMsg['mapa'] + self.sepMsg + self.mapa))
def getMapa(self): def getMapa(self):
...@@ -79,7 +90,11 @@ class MensageInterfaz: ...@@ -79,7 +90,11 @@ class MensageInterfaz:
def sendPedido(self,pedido): def sendPedido(self,pedido):
self.conex.publicar(self.topicSend,self.prefMsg['pedido'] + self.sepMsg + str(pedido)) msg = ""
for pos in pedido:
for num in pos:
msg = msg + str(num)
self.conex.publicar(self.topicSend[0],self.prefMsg['pedido'] + self.sepMsg + msg)
self.pedidoSolicitado = False self.pedidoSolicitado = False
...@@ -94,6 +109,3 @@ class MensageInterfaz: ...@@ -94,6 +109,3 @@ class MensageInterfaz:
\ No newline at end of file
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