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

Add MqTT handler #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

tbnobody
Copy link

Allows control of the matrix via mqtt instead of the web gui

@rralf
Copy link
Member

rralf commented Jul 13, 2018

With this, we can now remove the web server, right?

@tbnobody
Copy link
Author

Thats right. it allows to set the same animations as the webserver and also handles shutdown/reboot.

Copy link
Member

@rralf rralf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of changing the crappy webinterface to mqtt. Nevertheless, even the webinterface contains some ugly mess of which we should get rid off. It's a good time to do it now.

Please remove the webinterface as well if we don't use it any longer. We especially need to test robustness: what happens if the mqtt server isn't reachable when starting the application (e.g., network cable disconnect on boot) or disappears spuriously (e.g., network disconnect, mosquittod reboot, ...)

I'll test those changes asap.

install(FILES matrix-mqtt
DESTINATION sbin/
PERMISSIONS WORLD_EXECUTE WORLD_READ OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing newline

@@ -0,0 +1,90 @@
#!/usr/bin/env python3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we missed this so far -- please add a license header when creating new files. I'll amend this in existing files soon…


def on_message_shutdown(client, userdata, message):
if not message.retain:
system("poweroff")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use systemd-poweroff service to power off the system and grant the user poweroff abilities? This would avoid running the script as root.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know that this is even possible. Will search for some documentation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you run a KDE session and press the shutdown button, then your user also must somehow have the permission to shutdown the system.

Seems liks this is done via dbus or console-kit.

That's worth a look:
https://stackoverflow.com/questions/23013274/shutting-down-computer-linux-using-python

Maybe we can instrument python to perform the shutdown directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a good idea, though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks a bit easier: https://wiki.ubuntuusers.de/systemd/systemctl/#System-Kommandos
Just call systemctl reboot

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! We only need to make sure that a user may invoke this command.

TOPIC_REBOOT = TOPIC_BASE + 'reboot'
TOPIC_SHUTDOWN = TOPIC_BASE + 'shutdown'

# no root no fun.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm… This has been a mess ever since. We should change that.

mqttc.connect_async(MQTT_HOSTNAME)

try:
mqttc.loop_forever()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the mqtt server suddenly disappears? Will the client automatically reconnect on connection failures or stall/crash?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's test it, but based on the documentation a reconnect will be performed until its connected again

try:
mqttc.loop_forever()
except KeyboardInterrupt:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A keyboard interrupt will break the loop?!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I applied the same logic as you did in the web server

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Hmm, don't know why I handles this exception. Maybe for debugging. Could be that a keyboard interrupt will end the loop. The catch of the exception will shutdown the webserver without failing. Don't think we need this for mqtt.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm that's required to quit the loop by pressing ctrl+c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants