Обработка ошибок обрыва соединения.
authorRoman Bazalevskiy <rvb@rvb.name>
Mon, 15 Nov 2021 07:52:46 +0000 (10:52 +0300)
committerRoman Bazalevskiy <rvb@rvb.name>
Mon, 15 Nov 2021 07:52:46 +0000 (10:52 +0300)
voicecontrol

index 703e109f9920b3858979ebf37d5ea5b0b7835bc8..7216fef829eee8765ed549cea6ae96a4a3b196f3 100755 (executable)
@@ -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):