-
Notifications
You must be signed in to change notification settings - Fork 2
/
unlock.py
43 lines (36 loc) · 1.06 KB
/
unlock.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
from __future__ import print_function
from paho import mqtt
import paho.mqtt.client as paho
from random import randrange
from functools import partial
from time import sleep
import hackhub
unlock_done = False
def on_msg(msg_id, c, ud, msg):
global unlock_done
m = msg.payload.decode("utf-8")
if "ACK" in m:
unlock_done = True
def unlock(user=None):
global unlock_done
unlock_done = False
c = paho.Client(client_id="hackhub", userdata=None, protocol=paho.MQTTv5)
c.username_pw_set(hackhub.MQTT_BROKER_USER, hackhub.MQTT_BROKER_PW)
msg_id = randrange(int(1e9))
c.on_message = partial(on_msg, msg_id)
c.tls_set()
c.connect(hackhub.MQTT_BROKER, hackhub.MQTT_BROKER_PORT)
c.subscribe("cmd")
c.publish("cmd", "open")
c.loop_start()
times = 0
while not unlock_done:
if times > 4000:
c.loop_stop()
return False, "Timeout waiting for lock to ACK command"
times += 1
sleep(0.001)
c.loop_stop()
return True, "unlocked"
if __name__=="__main__":
print(unlock())