]> git.0d.be Git - django-panik-nonstop.git/commitdiff
switch-jack: use dedicated methods with error handling for dis/connections
authorFrédéric Péters <fpeters@0d.be>
Thu, 19 Oct 2023 16:00:21 +0000 (18:00 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 19 Oct 2023 16:00:21 +0000 (18:00 +0200)
nonstop/management/commands/switch-jack.py

index bbeba1de680d98484b6288690d1bb52719860be4..a64e573957c8511e22cd3980820a6c677a1651a9 100644 (file)
@@ -48,29 +48,35 @@ class Command(BaseCommand):
             return
         logging.info('setting source: %s', active)
         dports = app_settings.SWITCH_OUT_PORTS
-        try:
-            with jack.Client('switch-jack') as client:
-                known_ports = {x.name for x in client.get_ports(is_audio=True)}
-                if any(x not in known_ports for x in dports):
-                    logging.error('unavailable destination ports %r', dports)
-                    return
-                for port_id, port_names in app_settings.SWITCH_IN_PORTS.items():
-                    if any(x not in known_ports for x in port_names):
-                        logging.error('unavailable source ports %r', port_names)
-                        continue
+        with jack.Client('switch-jack') as client:
+            known_ports = {x.name for x in client.get_ports(is_audio=True)}
+            if any(x not in known_ports for x in dports):
+                logging.error('unavailable destination ports %r', dports)
+                return
+            for port_id, port_names in app_settings.SWITCH_IN_PORTS.items():
+                if any(x not in known_ports for x in port_names):
+                    logging.error('unavailable source ports %r', port_names)
+                    continue
+                try:
                     if port_id == active:
-                        if self.verbosity:
-                            logging.info('connecting %s and %s', port_names[0], dports[0])
-                        client.connect(port_names[0], dports[0])
-                        if self.verbosity:
-                            logging.info('connecting %s and %s', port_names[1], dports[1])
-                        client.connect(port_names[1], dports[1])
+                        self.jack_connect(client, port_names[0], dports[0])
+                        self.jack_connect(client, port_names[1], dports[1])
                     else:
-                        if self.verbosity:
-                            logging.info('disconnecting %s and %s', port_names[0], dports[0])
-                        client.disconnect(port_names[0], dports[0])
-                        if self.verbosity:
-                            logging.info('disconnecting %s and %s', port_names[1], dports[1])
-                        client.disconnect(port_names[1], dports[1])
-        except jack.JackError as e:
-            logging.error('jack error: %s' % e)
+                        self.jack_disconnect(client, port_names[0], dports[0])
+                        self.jack_disconnect(client, port_names[1], dports[1])
+                except jack.JackError as e:
+                    logging.error('jack error: %s' % e)
+
+    def jack_connect(self, client, in_port, out_port):
+        connections = [x.name for x in client.get_all_connections(in_port)]
+        if out_port not in connections:
+            if self.verbosity:
+                logging.info('connecting %s and %s', in_port, out_port)
+            client.connect(in_port, out_port)
+
+    def jack_disconnect(self, client, in_port, out_port):
+        connections = [x.name for x in client.get_all_connections(in_port)]
+        if out_port in connections:
+            if self.verbosity:
+                logging.info('disconnecting %s and %s', in_port, out_port)
+            client.disconnect(in_port, out_port)