-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (52 loc) · 1.98 KB
/
app.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
import paho.mqtt.client as mqtt
from pynput.keyboard import Key, Controller
import json, os
DEBUG=True
TOPIC='zigbee2mqtt/+/action'
#########################################################################
def on_connect(client, userdata, flags, rc):
printc("[+] Connected with result code "+str(rc))
client.subscribe(TOPIC)
printc("[+] Subscribed to " + TOPIC)
def on_message(client, userdata, msg):
_, device, _ = msg.topic.split('/')
if ((device in config['devices'])):
action = msg.payload.decode()
if (action in config['devices'][device]):
to_stroke = config['devices'][device][action]
if(isArray(to_stroke)):
print_message(device, action, to_stroke)
with keyboard.pressed(*list(map(lambda k: Key[k], to_stroke[:-1]))):
press_key(to_stroke[-1])
else:
print_message(device, action, to_stroke)
press_key(to_stroke)
else:
print_unmapped(device, action)
def press_key(key_name):
if(key_name in Key.__members__):
key = Key[key_name]
else:
key = key_name
keyboard.press(key)
keyboard.release(key)
def isArray(var):
return isinstance(var, (list, tuple))
def print_message(device, action, to_stroke):
printc("[+] "+device+": "+action+" --> " + str(to_stroke))
def print_unmapped(device, action):
printc("[!] "+device+": "+action+" is unmapped!")
def printc(s):
if(DEBUG):
print(s)
#########################################################################
dir_path = os.path.dirname(os.path.realpath(__file__)) + '/'
with open(dir_path + 'config.json') as f:
config = json.load(f)
keyboard = Controller()
client = mqtt.Client()
client.username_pw_set(config['connection']['username'],config['connection']['password'])
client.on_connect = on_connect
client.on_message = on_message
client.connect(config['connection']['host'], config['connection']['port'], 60)
client.loop_forever()