From 40d3028fd97291c60a966792ef2efdde114b8e71 Mon Sep 17 00:00:00 2001 From: Roman Bazalevskiy Date: Mon, 15 Nov 2021 10:52:46 +0300 Subject: [PATCH 1/1] =?utf8?q?=D0=9E=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?utf8?q?=D0=BA=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20=D0=BE?= =?utf8?q?=D0=B1=D1=80=D1=8B=D0=B2=D0=B0=20=D1=81=D0=BE=D0=B5=D0=B4=D0=B8?= =?utf8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- voicecontrol | 90 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/voicecontrol b/voicecontrol index 703e109..7216fef 100755 --- a/voicecontrol +++ b/voicecontrol @@ -48,7 +48,7 @@ def SkipSource(source,seconds): global config try: if config["debug"]: - print("Skipping: ", seconds) + print("Skipping: ", seconds) bufs = int((seconds)*source._rate/source._frames_per_buffer) for i in range(bufs): buffer = source.read(source._frames_per_buffer) @@ -124,49 +124,68 @@ def RunCommand(command, pyaud, mic = None): if command_url: try: + if config["debug"]: + print('Preparing command') if command_user: my_headers = urllib3.util.make_headers(basic_auth=command_user+':'+command_pwd) else: my_headers = urllib3.util.make_headers() my_headers['Content-Type']='text/plain' my_headers['Accept']='apllication/json' - http.request('POST',command_url,headers=my_headers,body=command.encode('UTF-8')) - if reply_url: - sleep(0.5) - res="NULL" - for i in range(api_attempts): - try: - if command_user: - my_headers = urllib3.util.make_headers(basic_auth=command_user+':'+command_pwd) - else: - my_headers = urllib3.util.make_headers() - req=http.request('GET',reply_url,headers=my_headers).data - res = json.loads(req)['state'].strip() - if config["debug"]: - print(res) - if not(res == 'NULL'): - break - sleep(1) - except KeyboardInterrupt: - raise - except: - sleep(1) - if res and not(res=="NULL"): - PlayBack(pyaud, res, mic=mic) - elif res=="NULL": - PlayBack(pyaud, "Сервер не ответил", mic=mic) - if command_user: - my_headers = urllib3.util.make_headers(basic_auth=command_user+':'+command_pwd) + if config["debug"]: + print('Sending command') + sent = False + for i in range(api_attempts): + try: + http.request('POST',command_url,headers=my_headers,body=command.encode('UTF-8')) + sent = True + break + except Exception as e: + print('Exception: '+str(e)) + sleep(0.5) + if sent: + if config["debug"]: + print('Command sent') + if reply_url: + sleep(0.5) + res="NULL" + for i in range(api_attempts): + try: + if command_user: + my_headers = urllib3.util.make_headers(basic_auth=command_user+':'+command_pwd) + else: + my_headers = urllib3.util.make_headers() + req=http.request('GET',reply_url,headers=my_headers).data + res = json.loads(req)['state'].strip() + if config["debug"]: + print(res) + if not(res == 'NULL'): + break + sleep(1) + except KeyboardInterrupt: + raise + except Exception as e: + print('Exception: '+str(e)) + sleep(1) + if res and not(res=="NULL"): + PlayBack(pyaud, res, mic=mic) + elif res=="NULL": + PlayBack(pyaud, "Сервер не ответил", mic=mic) + if command_user: + my_headers = urllib3.util.make_headers(basic_auth=command_user+':'+command_pwd) + else: + my_headers = urllib3.util.make_headers() + my_headers['Content-Type']='text/plain' + my_headers['Accept']='apllication/json' + command="" + http.request('POST',command_url, headers=my_headers, body=command.encode('UTF-8')) else: - my_headers = urllib3.util.make_headers() - my_headers['Content-Type']='text/plain' - my_headers['Accept']='apllication/json' - command="" - http.request('POST',command_url, headers=my_headers, body=command.encode('UTF-8')) + PlayBack(pyaud, "Сервер недоступен", mic=mic) except KeyboardInterrupt: raise - except: + except Exception as e: try: + print('Exception: '+str(e)) http.request('POST',command_url, headers=my_headers, body="") except: pass @@ -282,7 +301,8 @@ async def main_loop(uri): PlayBack(p, "Так команду и не поняла...", mic=s) except KeyboardInterrupt: raise - except: + except Exception as e: + print('Exception: '+str(e)) pass def get_config(path): -- 2.34.1