Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openvisualizer/rpl/topology.py: publish topology data #53

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openvisualizer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(self, host, port, simulator_mode, debug, vcdlog,
self.openlbr = openlbr.OpenLbr(use_page_zero)
self.rpl = rpl.RPL()
self.jrc = jrc.JRC()
self.topology = topology.Topology()
self.topology = topology.Topology(mqtt_broker)
self.mote_probes = []

# create opentun call last since indicates prefix
Expand Down
43 changes: 40 additions & 3 deletions openvisualizer/rpl/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import threading
import time

import struct
import json
import paho.mqtt.client as mqtt

from openvisualizer.eventbus.eventbusclient import EventBusClient

log = logging.getLogger('Topology')
Expand All @@ -26,11 +30,11 @@

class Topology(EventBusClient):

def __init__(self):
def __init__(self, mqtt_broker):

# log
log.debug('create instance')

log.debug('create instance')
# local variables
self.data_lock = threading.Lock()
self.parents = {}
Expand All @@ -53,6 +57,39 @@ def __init__(self):
],
)

self.broker = mqtt_broker
self.mqtt_connected = False

if self.broker:

# connect to MQTT
self.mqtt_client = mqtt.Client()
self.mqtt_client.on_connect = self._on_mqtt_connect

try:
self.mqtt_client.connect(self.broker)
except Exception as e:
log.error("failed to connect to {} with error msg: {}".format(self.broker, e))
else:
# start mqtt client
self.mqtt_thread = threading.Thread(name='mqtt_loop_thread', target=self.mqtt_client.loop_forever)
self.mqtt_thread.start()


def publish_topology(self):
payload = {'token': 123}
payload['topology'] = str(self.parents)

if self.mqtt_connected:
# publish the cmd message
self.mqtt_client.publish(topic='opentestbed/openv-server/topology', payload=json.dumps(payload), qos=2)


# ======================== private =========================================

def _on_mqtt_connect(self, client, userdata, flags, rc):
self.mqtt_connected = True

# ======================== public ==========================================

def get_parents(self, sender, signal, data):
Expand Down Expand Up @@ -84,7 +121,7 @@ def update_parents(self, sender, signal, data):
# data[0] == source address, data[1] == list of parents
self.parents.update({data[0]: data[1]})
self.parents_last_seen.update({data[0]: time.time()})

self.publish_topology()
self._clear_node_timeout()

def _clear_node_timeout(self):
Expand Down