Commit aef7b635 by almagosi

Creacion fichero configuracion y clases abstraidas para la comunicación

parent 3ef52cdb
import paho.mqtt.client as mqtt
import json
class Conexion:
......@@ -6,12 +7,15 @@ class Conexion:
def __init__(self, topics,on_msg):
with open(file="conexionConfig.json",mode='r') as f:
data = json.load(f)
self.__broker_address = data['ipbroker']
self.__port = data['portbroker']
# 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
#self.__broker_address = "192.168.0.100" # Cambia por la dirección de tu broker MQTT
self.__port = 1883 # Puerto predeterminado para MQTT
#self.__port = 1883 # Puerto predeterminado para MQTT
#self.__topic = ["A3-467/GrupoL","map"]
self.__topic = topics
......
{
"ipbroker" :"192.168.48.245",
"portbroker" : "1883",
"topicSubsRobot" : "A3-467/GrupoL/Robot",
"topicSubsInterfaz" : ["A3-467/GrupoL/Interfaz","map"],
"topicSendRobot" : "A3-467/GrupoL/Interfaz",
"topicSendInterfaz" : "A3-467/GrupoL/Robot",
"separadorMensaje" : ":",
"prefijoMensajes" : {
"mapa":"map",
"pedido":"ped",
"posicion":"pos"
}
}
\ No newline at end of file
#from typing import Any
import conexion as cn
import time
import json
class MensageInterfaz:
def __init__(self) -> None:
def filtrarMensage(topic,mensaje):
if(topic == self.prefMsg['mapa'] and len(self.mapa) == 0):
self.mapa = mensaje
self.__enviarMapa()
else:
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])
def on_message(client, userdata, message):
#print(f'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:
data = json.load(f)
#self.topicSubs = ["A3-467/GrupoL/Robot"]
self.topicSubs = data['topicSubsInterfaz']
#self.topicSend = ["A3-467/GrupoL/Interfaz"]
self.topicSend = data['topicSendInterfaz']
#self.prefMsg = {"mapa":'map',
# "pedido":'ped',
# "posicion":'pos'}
self.prefMsg = data['prefijoMensajes']
self.sepMsg = data['separadorMensaje']
self.conex = cn.Conexion(self.topicSubs,on_msg=on_message)
self.mapa = ""
self.posicion : list
self.pedidoSolicitado = False
def __enviarMapa(self):
self.conex.publicar(self.topicSend,self.prefMsg['mapa'] + self.sepMsg + self.mapa)
def getMapa(self):
while(len(self.mapa) == 0):
time.sleep(1)
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))
def getPosicion(self):
return self.posicion
\ No newline at end of file
#from typing import Any
import conexion as cn
import time
import json
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"))
with open(file="conexionConfig.json",mode='r') as f:
data = json.load(f)
#self.topicSubs = ["A3-467/GrupoL/Robot"]
self.topicSubs = data['topicSubsRobot']
#self.topicSend = ["A3-467/GrupoL/Interfaz"]
self.topicSend = data['topicSendRobot']
#self.prefMsg = {"mapa":'map',
# "pedido":'ped',
# "posicion":'pos'}
self.prefMsg = data['prefijoMensajes']
self.sepMsg = data['separadorMensaje']
self.conex = cn.Conexion(self.topicSubs,on_msg=on_message)
self.mensaje = ""
def getMapa(self):
while(self.mensaje.split(self.sepMsg)[0] != self.prefMsg['mapa']):
time.sleep(1)
return self.mensaje.split(self.sepMsg)[1]
def getPedido(self):
self.conex.publicar(self.topicSend,self.prefMsg['pedido'] + self.sepMsg)
while(self.mensaje.split(self.sepMsg)[0] != self.prefMsg['pedido']):
time.sleep(1)
return self.mensaje.split(self.sepMsg)[1]
def sendPosicion(self,posicion):
self.conex.publicar(self.topicSend,self.prefMsg['posicion'] + self.sepMsg + str(posicion))
\ 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