-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.py
101 lines (75 loc) · 2.56 KB
/
listener.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import sys
if sys.version_info[0] < 3:
import Queue as queue
else:
import queue
import threading
import signal
import numpy
import pyaudio
from quiet.quiet import Decoder
class Listener(object):
def __init__(self):
self.pyaudio_instance = None
self.done = None
self.thread = None
def start(self):
self.done = False
if not (self.thread and self.thread.is_alive()):
self.thread = threading.Thread(target=self.run)
self.thread.start()
def run(self):
FORMAT = pyaudio.paFloat32
CHANNELS = 1
RATE = 44100
CHUNK = int(RATE / 10)
if not self.pyaudio_instance:
self.pyaudio_instance = pyaudio.PyAudio()
q = queue.Queue()
def callback(in_data, frame_count, time_info, status):
q.put(in_data)
return (None, pyaudio.paContinue)
stream = self.pyaudio_instance.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
stream_callback=callback)
decoder = Decoder(profile_name='ultrasonic-experimental')
while not self.done:
audio = q.get()
audio = numpy.fromstring(audio, dtype='float32')
data = decoder.decode(audio)
if data is not None:
self.on_data(data)
stream.stop_stream()
def stop(self):
self.done = True
if self.thread and self.thread.is_alive():
self.thread.join()
def on_data(self, data):
print(data)
def main():
listener = Listener()
def on_data(data):
ssid_length = data[0]
ssid = data[1:ssid_length+1].tostring().decode('utf-8')
password = data[ssid_length+1:].tostring().decode('utf-8')
print('SSID: {}\nPassword: {}'.format(ssid, password))
if os.system('which nmcli >/dev/null') == 0:
cmd = 'sudo nmcli device wifi connect {} password {}'.format(ssid, password)
if os.system(cmd) == 0:
print('Wi-Fi is connected')
listener.stop()
else:
print('Failed')
else:
print('to do')
def int_handler(sig, frame):
listener.stop()
signal.signal(signal.SIGINT, int_handler)
listener.on_data = on_data
listener.run()
if __name__ == '__main__':
main()