Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cziter15 committed Sep 10, 2023
1 parent ce73eb9 commit 1330106
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/custom_components/mr6400sms/devices/__pycache__/*.pyc
/custom_components/mr6400sms/test.py
8 changes: 4 additions & 4 deletions custom_components/mr6400sms/devices/mr6400.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def _perform_login(self):
url = self._buildUrl('cgi/getParm')
headers = {'Referer': self._baseurl}

async with self._websession.post(url, timeout=10, headers=headers) as response:
async with self._websession.post(url, timeout=self._requestTimeout, headers=headers) as response:
if response.status != 200:
raise RouterException(f"Invalid encryption key request because of status {response.status}")

Expand All @@ -64,7 +64,7 @@ async def _perform_login(self):
}
headers = { 'Referer': self._baseurl }

async with self._websession.post(url, params=params, headers=headers, timeout=10) as response:
async with self._websession.post(url, params=params, headers=headers, timeout=self._requestTimeout) as response:
if response.status != 200:
raise RouterException("Invalid login request")

Expand All @@ -75,7 +75,7 @@ async def _perform_login(self):

url = self._buildUrl('')

async with self._websession.get(url,timeout=10) as response:
async with self._websession.get(url,timeout=self._requestTimeout) as response:
if response.status != 200:
raise RouterException("Invalid token request because of status " + str(response.status))

Expand All @@ -100,6 +100,6 @@ async def _send_sms(self, phone, message):
data = ("[LTE_SMS_SENDNEWMSG#0,0,0,0,0,0#0,0,0,0,0,0]0,3\r\n"f"index=1\r\nto={phone}\r\ntextContent={message}\r\n")
headers = {'Referer': self._baseurl, 'TokenID': self._token}

async with self._websession.post(url, params=params, data=data, headers=headers, timeout=10) as response:
async with self._websession.post(url, params=params, data=data, headers=headers, timeout=self._requestTimeout) as response:
if response.status != 200:
raise RouterException("Failed sending SMS")
27 changes: 19 additions & 8 deletions custom_components/mr6400sms/devices/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RouterException(Exception):
pass

class Router:
def __init__(self, hostname, username, password):
def __init__(self, hostname, username, password, maxRetries=3, requestTimeout=10):
"""
Initializes the router.
"""
Expand All @@ -17,14 +17,27 @@ def __init__(self, hostname, username, password):
self._username = username
self._password = password
self._baseurl = "http://{}/".format(self._hostname)
self._maxRetries = 3
self._maxRetries = maxRetries
self._requestTimeout = requestTimeout

def _buildUrl(self, path):
"""
Builds the URL for the router.
"""
return self._baseurl + path

async def _perform_login(self):
"""
This function is overridden in child classes. Implements per-device login flow.
"""
pass

async def _perform_logout(self):
"""
This function is overridden in child classes. Implements per-device logout flow.
"""
pass

async def login(self):
"""
Login to the router.
Expand All @@ -35,7 +48,7 @@ async def login(self):
try:
await self._perform_login()
break
except Exception as e: #TOOD: Non-general exception
except (RouterException, Exception) as e:
if retries < self._maxRetries - 1:
_LOGGER.warning("Retrying login (%d of %d) due to exception {%s}", retries + 1, self._maxRetries, e)
await asyncio.sleep(1)
Expand All @@ -45,17 +58,15 @@ async def logout(self):
Logs out the user by closing the web session.
"""
if self._websession is not None:
await self._perform_logout()
await self._websession.close()
self._websession = None
pass

async def send_message(self, phone_numbers, message):
"""
Sends a message to a list of phone numbers.
Sends a message to a list of given phone numbers.
"""
for phone in phone_numbers:
await self._send_sms(phone, message)
_LOGGER.info("Sent SMS to %s: %s", phone, message)

async def perform_login(self):
raise NotImplementedError()
_LOGGER.info("Sent SMS to %s: %s", phone, message)

0 comments on commit 1330106

Please sign in to comment.