]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/asterisk.py
11 from .decorators
import *
13 loop
= asyncio
.get_event_loop()
15 class Asterisk(misc
.Object
):
19 # Connect as soon as the event loop starts
20 loop
.create_task(self
.connect())
24 if not self
.__manager
:
25 raise RuntimeError("Asterisk is not connected")
29 async def connect(self
):
33 manager
= panoramisk
.Manager(
34 host
= self
.settings
.get("asterisk-ami-host"),
35 username
= self
.settings
.get("asterisk-ami-username"),
36 secret
= self
.settings
.get("asterisk-ami-secret"),
38 on_connect
=self
._on
_connect
,
42 await manager
.connect()
46 def _on_connect(self
, manager
):
47 logging
.debug("Connection to Asterisk established")
49 # Close any existing connections
51 self
.__manager
.close()
53 self
.__manager
= manager
56 manager
= await self
.manager()
58 result
= manager
.ping()
61 async def get_sip_channels(self
, filter=None):
64 for data
in await self
.manager
.send_action({"Action" : "CoreShowChannels"}):
65 # Skip header and trailer
70 channel
= Channel(self
.backend
, data
)
73 if filter and not channel
.matches(filter):
76 channels
.append(channel
)
80 async def get_registrations(self
, filter=None):
83 for data
in await self
.manager
.send_action({"Action" : "PJSIPShowContacts"}):
84 # Skip header and trailer
89 registration
= Registration(self
.backend
, data
)
92 if filter and not registration
.matches(filter):
95 registrations
.append(registration
)
100 class Channel(misc
.Object
):
101 def init(self
, data
):
105 return self
.connected_line
108 def account_code(self
):
109 return self
.data
.AccountCode
112 def connected_line(self
):
113 return self
.data
.ConnectedLineName
or self
.data
.ConnectedLineNum
115 def matches(self
, filter):
117 self
.data
.CallerIDNum
,
122 h
, m
, s
= self
.data
.Duration
.split(":")
125 h
, m
, s
= int(h
), int(m
), int(s
)
129 return datetime
.timedelta(hours
=h
, minutes
=m
, seconds
=s
)
131 def is_connected(self
):
132 return self
.data
.ChannelStateDesc
== "Up"
134 def is_ringing(self
):
135 return self
.data
.ChannelStateDesc
== "Ringing"
138 class Registration(misc
.Object
):
139 def init(self
, data
):
142 def __lt__(self
, other
):
143 if isinstance(other
, self
.__class
__):
144 if isinstance(self
.user
, accounts
.Account
):
145 if isinstance(other
.user
, accounts
.Account
):
146 return self
.user
< other
.user
148 return self
.user
.name
< other
.user
150 if isinstance(other
.user
, accounts
.Account
):
151 return self
.user
< other
.user
.name
153 return self
.user
< other
.user
155 return NotImplemented
158 return self
.user_agent
160 def matches(self
, filter):
161 return self
.data
.Endpoint
== filter
165 return urllib
.parse
.urlparse(self
.data
.Uri
)
168 def uri_params(self
):
171 for param
in self
.uri
.params
.split(";"):
172 key
, _
, value
= param
.partition("=")
180 return self
.uri_params
.get("transport")
184 return self
.backend
.accounts
.get_by_sip_id(self
.data
.Endpoint
) or self
.data
.Endpoint
189 user
, _
, address
= self
.uri
.path
.partition("@")
192 address
, _
, port
= address
.rpartition(":")
197 def user_agent(self
):
198 return self
.data
.UserAgent
.replace("_", " ")
202 return int(self
.data
.RoundtripUsec
) / 1000