from time import sleep
from flask import Flask, Response,render_template
import pyaudio
import socketio
# from urllib.parse import quote
from urllib.request import urlopen
import wave
from contextlib import closing
import threading
from urllib.error import URLError
app = Flask(__name__,
static_url_path='',
template_folder='templates/'
)
sio = socketio.Client()
deviceId = "UDK-4"
player = None
def playwav(path_or_file):
global player
if player is None:
player = pyaudio.PyAudio()
with closing(wave.open(path_or_file, 'rb')) as wavfile, \
closing(player.open(
format=player.get_format_from_width(wavfile.getsampwidth()),
channels=wavfile.getnchannels(),
rate=wavfile.getframerate(),
output=True)) as stream:
while True:
data = wavfile.readframes(1024) # read 1024 frames at once
if not data: # EOF
break
stream.write(data)
@sio.event
def connect():
print('connection established')
sio.emit("car_push_connect",deviceId)
@sio.event
def audio_ready(data):
print("event geldi",data)
if data == deviceId:
playwav(urlopen(f'http://172.29.0.10:6006/Service/pushtotalk/{deviceId}/audio_stream'.format(**vars())))
@sio.event
def disconnect():
print('disconnected from server')
def connTh():
global player
conn = sio.connect('https://udk.securitas.com.tr:6004')
print(str(conn))
sleep(5)
threading.Thread(target=connTh).start()
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=False, threaded=True,port=4000)