2022-01-06 16:11:21 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-01-17 12:20:54 +01:00
|
|
|
from .connection import Connection
|
2022-05-01 13:13:31 +02:00
|
|
|
from . import exceptions
|
2022-01-06 16:11:21 +01:00
|
|
|
from . import messages
|
|
|
|
|
|
|
|
class OMMClient2:
|
|
|
|
"""
|
|
|
|
High level wrapper for the OM Application XML Interface
|
|
|
|
|
|
|
|
This class tries to provide functions for often used methods without the
|
|
|
|
need of using the underlying messaging protocol
|
|
|
|
|
|
|
|
:param host: Hostname or IP address of the OMM
|
|
|
|
:param username: Username
|
|
|
|
:param password: Password
|
2022-01-17 12:20:54 +01:00
|
|
|
:param port: Port where to access the API, if None, use default value
|
|
|
|
:param ommsync: If True login as OMM-Sync client. Some operations in OMM-Sync mode might lead to destroy DECT paring.
|
2022-01-06 16:11:21 +01:00
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
|
|
|
>>> c = OMMClient2("omm.local", "admin", "admin")
|
|
|
|
>>> c.ping()
|
|
|
|
|
2022-01-17 12:20:54 +01:00
|
|
|
Use request to send custom messages::
|
2022-01-06 16:11:21 +01:00
|
|
|
|
2022-04-02 21:10:05 +02:00
|
|
|
>>> r = s.connection.request(mitel_ommclient2.messages.Ping())
|
2022-01-17 12:20:54 +01:00
|
|
|
"""
|
2022-01-06 16:11:21 +01:00
|
|
|
|
2022-01-17 12:20:54 +01:00
|
|
|
def __init__(self, host, username, password, port=None, ommsync=False):
|
|
|
|
self._host = host
|
|
|
|
self._username = username
|
|
|
|
self._password = password
|
|
|
|
self._port = port
|
|
|
|
self._ommsync = ommsync
|
2022-01-06 16:11:21 +01:00
|
|
|
|
2022-01-17 12:20:54 +01:00
|
|
|
# prepare connect arguments
|
|
|
|
kwargs = {}
|
|
|
|
if self._port is not None:
|
|
|
|
kwargs["port"] = self._port
|
|
|
|
|
|
|
|
# Connect
|
2022-04-02 21:10:05 +02:00
|
|
|
self.connection = Connection(self._host, **kwargs)
|
|
|
|
self.connection.connect()
|
2022-01-17 12:20:54 +01:00
|
|
|
|
|
|
|
# Login
|
2022-04-02 21:10:05 +02:00
|
|
|
r = self.connection.request(messages.Open(self._username, self._password, UserDeviceSyncClient=self._ommsync))
|
2022-01-17 12:20:54 +01:00
|
|
|
r.raise_on_error()
|
|
|
|
|
2022-01-06 18:13:48 +01:00
|
|
|
def get_account(self, id):
|
|
|
|
"""
|
|
|
|
Get account
|
|
|
|
|
|
|
|
:param id: User id
|
|
|
|
"""
|
|
|
|
|
2022-04-02 21:10:05 +02:00
|
|
|
r = self.connection.request(messages.GetAccount(id))
|
2022-01-06 18:13:48 +01:00
|
|
|
r.raise_on_error()
|
2022-01-06 23:58:59 +01:00
|
|
|
if r.account is None:
|
|
|
|
return None
|
2022-01-06 18:13:48 +01:00
|
|
|
return r.account[0]
|
|
|
|
|
2022-05-01 12:53:46 +02:00
|
|
|
def get_device(self, ppn):
|
2022-01-16 13:52:08 +01:00
|
|
|
"""
|
|
|
|
Get PP device
|
|
|
|
|
2022-05-01 12:50:59 +02:00
|
|
|
:param ppn: Device id
|
2022-01-16 13:52:08 +01:00
|
|
|
"""
|
2022-04-02 21:10:05 +02:00
|
|
|
r = self.connection.request(messages.GetPPDev(ppn))
|
2022-01-16 13:52:08 +01:00
|
|
|
r.raise_on_error()
|
|
|
|
if r.pp is None:
|
|
|
|
return None
|
|
|
|
return r.pp[0]
|
|
|
|
|
2022-05-01 13:13:31 +02:00
|
|
|
def get_devices(self):
|
|
|
|
"""
|
|
|
|
Get all PP devices
|
|
|
|
"""
|
|
|
|
next_ppn = 0
|
|
|
|
while True:
|
|
|
|
r = self.connection.request(messages.GetPPDev(next_ppn, maxRecords=20))
|
|
|
|
try:
|
|
|
|
r.raise_on_error()
|
|
|
|
except exceptions.ENoEnt:
|
|
|
|
# No more devices to fetch
|
|
|
|
break
|
|
|
|
|
|
|
|
# Output all found devices
|
|
|
|
for pp in r.pp:
|
|
|
|
yield pp
|
|
|
|
|
|
|
|
# Determine next possible ppn
|
|
|
|
next_ppn = int(pp["ppn"]) + 1
|
|
|
|
|
2022-05-01 13:27:56 +02:00
|
|
|
def get_user(self, uid):
|
|
|
|
"""
|
|
|
|
Get PP user
|
|
|
|
|
|
|
|
:param uid: User id
|
|
|
|
"""
|
|
|
|
r = self.connection.request(messages.GetPPUser(uid))
|
|
|
|
r.raise_on_error()
|
|
|
|
if r.user is None:
|
|
|
|
return None
|
|
|
|
return r.user[0]
|
|
|
|
|
|
|
|
def get_users(self):
|
|
|
|
"""
|
|
|
|
Get all PP users
|
|
|
|
"""
|
|
|
|
next_uid = 0
|
|
|
|
while True:
|
|
|
|
r = self.connection.request(messages.GetPPUser(next_uid, maxRecords=20))
|
|
|
|
try:
|
|
|
|
r.raise_on_error()
|
|
|
|
except exceptions.ENoEnt:
|
|
|
|
# No more devices to fetch
|
|
|
|
break
|
|
|
|
|
|
|
|
# Output all found devices
|
|
|
|
for user in r.user:
|
|
|
|
yield user
|
|
|
|
|
|
|
|
# Determine next possible ppn
|
|
|
|
next_uid = int(user["uid"]) + 1
|
2022-05-01 13:13:31 +02:00
|
|
|
|
2022-01-06 16:11:21 +01:00
|
|
|
def ping(self):
|
|
|
|
"""
|
|
|
|
Is OMM still there?
|
|
|
|
|
|
|
|
Returns `True` when response is received.
|
|
|
|
"""
|
|
|
|
|
2022-04-02 21:10:05 +02:00
|
|
|
r = self.connection.request(messages.Ping())
|
2022-01-06 16:11:21 +01:00
|
|
|
if r.errCode is None:
|
|
|
|
return True
|
|
|
|
return False
|