]> git.ipfire.org Git - ipfire.org.git/commitdiff
asterisk: Create a unified method to fetch objects
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 28 Jun 2023 16:12:16 +0000 (16:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 28 Jun 2023 16:12:16 +0000 (16:12 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/asterisk.py

index cbc118314f65bfe6ea5eed253358a193877fb8a9..d8c74bcab17b250c195fff9688218d5a7e5d60ad 100644 (file)
@@ -58,76 +58,40 @@ class Asterisk(misc.Object):
                result = manager.ping()
                print(result)
 
-       async def get_sip_channels(self, filter=None):
-               channels = []
+       async def _fetch(self, cls, action, filter=None):
+               objects = []
 
-               for data in await self.manager.send_action({"Action" : "CoreShowChannels"}):
-                       # Skip header and trailer
-                       if data.eventlist:
+               # Run the action and parse all messages
+               for data in await self.manager.send_action({ "Action" : action }):
+                       if not data.Event == cls.event:
                                continue
 
-                       # Parse channel
-                       channel = Channel(self.backend, data)
+                       # Create the object and append it to the list
+                       o = cls(self.backend, data)
 
-                       # Apply filter
-                       if filter and not channel.matches(filter):
+                       # Filter out anything unwanted
+                       if filter and not o.matches(filter):
                                continue
 
-                       channels.append(channel)
-
-               return channels
+                       objects.append(o)
 
-       async def get_registrations(self, filter=None):
-               registrations = []
+               return objects
 
-               for data in await self.manager.send_action({"Action" : "PJSIPShowContacts"}):
-                       # Skip header and trailer
-                       if data.eventlist:
-                               continue
-
-                       # Parse registration
-                       registration = Registration(self.backend, data)
-
-                       # Apply filter
-                       if filter and not registration.matches(filter):
-                               continue
-
-                       registrations.append(registration)
+       async def get_sip_channels(self, filter=None):
+               return await self._fetch(Channel, "CoreShowChannels", filter=filter)
 
-               return registrations
+       async def get_registrations(self, filter=None):
+               return await self._fetch(Registration, "PJSIPShowContacts", filter=filter)
 
        async def get_outbound_registrations(self):
-               registrations = []
-
-               for data in await self.manager.send_action({"Action" : "PJSIPShowRegistrationsOutbound"}):
-                       if not data.Event == "OutboundRegistrationDetail":
-                               continue
-
-                       registration = OutboundRegistration(self.backend, data)
-                       registrations.append(registration)
-
-               return registrations
+               return await self._fetch(OutboundRegistration, "PJSIPShowRegistrationsOutbound")
 
        async def get_queues(self):
-               queues = {}
-
                # Fetch all queues
-               for data in await self.manager.send_action({"Action" : "QueueSummary"}):
-                       if not data.Event == "QueueSummary":
-                               continue
-
-                       queue = Queue(self.backend, data)
-                       queues[queue.name] = queue
+               queues = { q.name : q for q in await self._fetch(Queue, "QueueSummary") }
 
                # Fetch all members
-               for data in await self.manager.send_action({"Action" : "QueueStatus"}):
-                       print(data)
-
-                       if not data.Event == "QueueMember":
-                               continue
-
-                       member = QueueMember(self.backend, data)
-
+               for member in await self._fetch(QueueMember, "QueueStatus"):
                        # Append to the matching queue
                        try:
                                queues[member.queue].members.append(member)
@@ -138,6 +102,8 @@ class Asterisk(misc.Object):
 
 
 class Channel(misc.Object):
+       event = "CoreShowChannel"
+
        def init(self, data):
                self.data = data
 
@@ -176,6 +142,8 @@ class Channel(misc.Object):
 
 
 class Registration(misc.Object):
+       event = "ContactList"
+
        def init(self, data):
                self.data = data
 
@@ -243,6 +211,8 @@ class Registration(misc.Object):
 
 
 class OutboundRegistration(misc.Object):
+       event = "OutboundRegistrationDetail"
+
        def init(self, data):
                self.data = data
 
@@ -274,6 +244,8 @@ class OutboundRegistration(misc.Object):
 
 
 class Queue(misc.Object):
+       event = "QueueSummary"
+
        def init(self, data):
                self.data = data
 
@@ -295,6 +267,8 @@ class Queue(misc.Object):
 
 
 class QueueMember(misc.Object):
+       event = "QueueMember"
+
        def init(self, data):
                self.data = data