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

Transport: fix last will message #58

Merged
merged 1 commit into from
Jul 12, 2019
Merged
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
9 changes: 7 additions & 2 deletions python_transport/wirepas_gateway/protocol/mqtt_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def __init__(self,
settings,
logger,
on_termination_cb=None,
on_connect_cb=None):
on_connect_cb=None,
last_will_topic=None,
last_will_data=None):
Thread.__init__(self)
self.daemon = True
self.running = False
Expand Down Expand Up @@ -60,6 +62,9 @@ def __init__(self,
settings.mqtt_password)
self._client.on_connect = self._on_connect

if last_will_topic is not None and last_will_data is not None:
self._set_last_will(last_will_topic, last_will_data)

try:
self._client.connect(settings.mqtt_hostname, settings.mqtt_port, keepalive=MQTTWrapper.KEEP_ALIVE_S)
except (socket.gaierror, ValueError) as e:
Expand Down Expand Up @@ -157,7 +162,7 @@ def _get_socket(self):
self._client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)
return self._client.socket()

def set_last_will(self, topic, data):
def _set_last_will(self, topic, data):
# Set Last wil message
self._client.will_set(topic, data, qos=2, retain=True)

Expand Down
13 changes: 7 additions & 6 deletions python_transport/wirepas_gateway/transport_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ def __init__(

self.whitened_ep_filter = settings.whitened_endpoints_filter

last_will_topic = TopicGenerator.make_status_topic(self.gw_id)
last_will_message = wirepas_messaging.gateway.api.StatusEvent(
self.gw_id, GatewayState.OFFLINE).payload

self.mqtt_wrapper = MQTTWrapper(
settings,
self.logger,
self._on_mqtt_wrapper_termination_cb,
self._on_connect
self._on_connect,
last_will_topic,
last_will_message
)

self.mqtt_wrapper.start()
Expand All @@ -77,18 +83,13 @@ def _on_mqtt_wrapper_termination_cb(self):
self.stop_dbus_client()

def _set_status(self):
event_offline = wirepas_messaging.gateway.api.StatusEvent(
self.gw_id, GatewayState.OFFLINE
)

event_online = wirepas_messaging.gateway.api.StatusEvent(
self.gw_id, GatewayState.ONLINE
)

topic = TopicGenerator.make_status_topic(self.gw_id)

self.mqtt_wrapper.publish(topic, event_online.payload, qos=1, retain=True)
self.mqtt_wrapper.set_last_will(topic, event_offline.payload)

def _on_connect(self):
# Register for get gateway info
Expand Down