SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. import json
  4. import paho.mqtt.client as mqtt
  5. import uuid
  6.  
  7. class Communication:
  8.  
  9.     def __init__(self, mqtt_client, planet):
  10.         self.client = mqtt_client
  11.         self.client.on_message = self.on_message
  12.         self.planet = planet
  13.  
  14.         self.client.on_connect = self.on_connect
  15.         self.client.on_message = self.on_message
  16.         self.client.username_pw_set('147', password = 'YiDFP8qjb4')
  17.         self.client.connect('mothership.inf.tu-dresden.de', port = 8883)
  18.         self.client.subscribe('explorer/147', qos = 1)
  19.  
  20.         self.client.loop_start()
  21.         self.client.loop_stop()
  22.         self.client.disconnect()
  23.         self.ready = {
  24.             "from": "client",
  25.             "type": "ready"
  26.         }
  27.         self.path = {
  28.             "from": "client",
  29.             "type": "path",
  30.             "payload": {
  31.                 "startX": 1,
  32.                 "startY": 2,
  33.                 "startDirection": "N",
  34.                 "endX": 3,
  35.                 "endY": 4,
  36.                 "endDirection": "S",
  37.                 "pathStatus": "blocked"
  38.                     }
  39.             }
  40.  
  41.  
  42.     def on_connect(self, client, userdata, flags, rc): #called automatically when the broker responds to the connection request
  43.         if rc == 0:
  44.             client.publish("explorer/147", json.dumps(self.ready))
  45.             print(json.dumps(self.ready))
  46.  
  47.  
  48.     def on_message(self, client,topic,  data, message): #called automatically upon a received message
  49.         self.topic = message.topic
  50.         msg = json.loads(message)
  51.         client.subscribe(topic, 1)
  52.         return msg
  53.  
  54.  
  55.     def send_message(self, topic, message):
  56.         message.topic = self.topic
  57.         self.send = json.dumps(message)
  58.         self.client.publish(topic, message, 1)
  59.  
  60.  
  61.     def kommunikationsabfrage(self, msg):
  62.    
  63.         self.send_message("channel", json.dumps(self.path))
  64.    
  65.         if msg["type"] == "planet":
  66.             planetName = msg["payload"]["planetName"]
  67.             channel = "planet/{}-147".format(planetName)
  68.             self.client.subscribe(channel, qos=1)
  69.             self.send_message(channel, json.dumps(self.path))
  70.    
  71.    
  72.         """if msg["type"] == "path": #korrektur in Zielkoordinaten?
  73.            <> = msg["payload"]["endX"]
  74.            <> = msg["payload"]["endY"]
  75.            <> = msg["payload"]["endDirection"]
  76.        """
  77.        
  78.         if msg["type"] == "pathUnveiled":
  79.             start = [int(msg["payload"]["startX"]), int(msg["payload"]["startY"])]
  80.             end = [int(msg["payload"]["endX"]), int(msg["payload"]["endY"])]
  81.             startDirection = str(msg["payload"]["startDirection"])
  82.             endDirection = str(msg["payload"]["endDirection"])
  83.             if msg["payload"]["pathStatus"] == "free":
  84.                 pathWeight = msg["payload"]["pathWeight"]
  85.             else:
  86.                 pathWeight = -1
  87.    
  88.             self.planet.add_path([start,startDirection], [end,endDirection], pathWeight )
  89.    
  90.         '''if msg["type"] == "pathSelect":
  91.            <> = msg["payload"]["startDirection"] #Direction zu befahren?
  92.        if msg["type"] == "target":
  93.            if ["targetX"] == pos.x  and ["targetY"] == pos.y :
  94.                client.publish("explorer/147", json.dumps(targetReached))
  95.        if msg["type"] == "done" #zurückkehren?
  96.        '''
  97. #randomly floating messages
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top