-
-
Notifications
You must be signed in to change notification settings - Fork 624
/
send_zmq_message.py
80 lines (62 loc) · 1.93 KB
/
send_zmq_message.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import unicode_literals
import json
import zmq
from argparse import ArgumentParser
from netifaces import interfaces, ifaddresses, AF_INET
from os import getenv
import redis
from time import sleep
def get_portal_url():
gateway = getenv('PORTAL_GATEWAY', '192.168.42.1')
port = getenv('PORTAL_LISTENING_PORT', None)
if port is None:
return gateway
else:
return f'{gateway}:{port}'
def get_message(action):
if action == 'setup_wifi':
data = {
'network': getenv('PORTAL_SSID'),
'ssid_pswd': getenv('PORTAL_PASSPHRASE', None),
'address': get_portal_url(),
}
return f'{action}&{json.dumps(data)}'
elif action == 'show_splash':
ip_addresses = get_ip_addresses()
return f'{action}&{json.dumps(ip_addresses)}'
def get_ip_addresses():
return [
i['addr']
for interface_name in interfaces()
for i in ifaddresses(interface_name).setdefault(
AF_INET, [{'addr': None}])
if interface_name in ['eth0', 'wlan0']
if i['addr'] is not None
]
def is_viewer_subscriber_ready(r):
is_ready = r.get('viewer-subscriber-ready')
if is_ready is None:
return False
else:
return bool(int(is_ready))
def main():
argument_parser = ArgumentParser()
argument_parser.add_argument(
'--action',
required=True,
choices=('setup_wifi', 'show_splash'),
help='Specify the ZeroMQ message to be sent.',
)
args = argument_parser.parse_args()
r = redis.Redis(host='127.0.0.1', decode_responses=True, port=6379, db=0)
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://0.0.0.0:10001')
sleep(1)
message = get_message(args.action)
while not is_viewer_subscriber_ready(r):
sleep(1)
continue
socket.send_string(f'viewer {message}')
if __name__ == '__main__':
main()