-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_agent.py
64 lines (50 loc) · 2.28 KB
/
mqtt_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# !/usr/bin/python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import os
CLIENT_ID = "x240" # ToDo
TOPIC = "laptop/x240" # ToDo
BROKER_ADDRESS = "" # ToDo
PORT = 1883 # ToDo
USER = "" # ToDo
PASSWORD = "" # ToDo
LAST_WILL = "off"
QOS = 1
actions = {
# (device, state) : command
# mqtt should look like:
# msg.topic = TOPIC/device
# msg.payload = state
# hardware
("monitor", "on") : "xrandr --output eDP-1 --mode 1366x768 --pos 3600x312 --rotate normal --output DP-1 --off --output HDMI-1 --off --output DP-2 --off --output HDMI-2 --off --output DP-2-1 --off --output DP-2-2 --mode 1680x1050 --pos 0x0 --rotate normal --output DP-2-3 --primary --mode 1920x1080 --pos 1680x0 --rotate normal",
("monitor", "off") : "xrandr --output eDP-1 --off --output DP-1 --off --output HDMI-1 --off --output DP-2 --off --output HDMI-2 --off --output DP-2-1 --off --output DP-2-2 --mode 1680x1050 --pos 0x0 --rotate normal --output DP-2-3 --primary --mode 1920x1080 --pos 1680x0 --rotate normal",
("laptop", "off") : "shutdown now",
# media keys
("media", "pause") : "xdotool key XF86AudioPlay",
("media", "next") : "xdotool key XF86AudioNext",
("media", "last") : "xdotool key XF86AudioPrev",
("media", "mute") : "xdotool key XF86AudioMute",
("media", "lower") : "xdotool key XF86AudioLowerVolume",
("media", "raise") : "xdotool key XF86AudioRaiseVolume",
}
def on_message(client, userdata, message):
# parse incoming message
msg = str(message.payload.decode("utf-8"))
action = message.topic.split("/")[2]
# find matching action
for a in actions:
if a[0] == action and a[1] == msg:
# execute matching command
os.system("export DISPLAY=:0.0;" + code)
def on_connect(client, userdata, flags, rc):
print("Connected to MQTT Broker: " + BROKER_ADDRESS)
client.subscribe(TOPIC + "/#")
if __name__ == "__main__":
client = mqtt.Client(client_id=CLIENT_ID)
client.username_pw_set(USER, PASSWORD)
client.on_connect = on_connect
client.on_message = on_message
client.will_set(TOPIC, LAST_WILL, qos=QOS, retain=False)
client.connect(BROKER_ADDRESS, PORT)
client.publish(TOPIC, "on", qos=QOS)
client.loop_forever()