Skip to content

Commit

Permalink
Merge pull request #9 from amelchio/connection-control
Browse files Browse the repository at this point in the history
Connection control
  • Loading branch information
amelchio authored Mar 30, 2019
2 parents 6bc45eb + 4da4af7 commit 79fd521
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 10 deletions.
59 changes: 50 additions & 9 deletions eternalegypt/eternalegypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class LB2120:

listeners = attr.ib(init=False, factory=list)
max_sms_id = attr.ib(init=False, default=None)
task = attr.ib(init=False, default=None)

@property
def _baseurl(self):
Expand Down Expand Up @@ -151,31 +150,73 @@ async def sms(self, phone, message):
async with self.websession.post(url, data=data) as response:
_LOGGER.debug("Sent message with status %d", response.status)

@autologin
async def delete_sms(self, sms_id):
"""Delete a message."""

def _config_call(self, key, value):
"""Set a configuration key to a certain value."""
url = self._url('Forms/config')
data = {
'sms.deleteId': sms_id,
key: value,
'err_redirect': '/error.json',
'ok_redirect': '/success.json',
'token': self.token
}
async with self.websession.post(url, data=data) as response:
return self.websession.post(url, data=data)

@autologin
async def connect_lte(self):
"""Do an LTE reconnect."""
async with self._config_call('wwan.connect', 'DefaultProfile') as response:
_LOGGER.debug("Connected to LTE with status %d", response.status)

@autologin
async def delete_sms(self, sms_id):
"""Delete a message."""
async with self._config_call('sms.deleteId', sms_id) as response:
_LOGGER.debug("Delete %d with status %d", sms_id, response.status)

@autologin
async def set_failover_mode(self, mode):
"""Set failover mode."""
modes = {
'auto': 'Auto',
'wire': 'WAN',
'mobile': 'LTE',
}

if mode not in modes.keys():
_LOGGER.error("Invalid mode %s not %s", mode, "/".join(modes.keys()))
return

async with self._config_call('failover.mode', modes[mode]) as response:
_LOGGER.debug("Set mode to %s", mode)

@autologin
async def set_autoconnect_mode(self, mode):
"""Set autoconnect mode."""
modes = {
'never': 'Never',
'home': 'HomeNetwork',
'always': 'Always',
}

if mode not in modes.keys():
_LOGGER.error("Invalid mode %s not %s", mode, "/".join(modes.keys()))
return

async with self._config_call('wwan.autoconnect', modes[mode]) as response:
_LOGGER.debug("Set mode to %s", mode)

def _build_information(self, data):
"""Read the bits we need from returned data."""
if 'wwan' not in data:
raise Error()

result = Information()

result.serial_number = data['general']['FSN']
result.usage = data['wwan']['dataUsage']['generic']['dataTransferred']
result.upstream = data['failover']['backhaul']
result.serial_number = data['general']['FSN']
result.connection = data['wwan']['connection']
result.wire_connected = 'Connected' if data['failover']['wanConnected'] else 'Disconnected'
result.mobile_connected = data['wwan']['connection']
result.connection_text = data['wwan']['connectionText']
result.connection_type = data['wwan']['connectionType']
result.current_nw_service_type = data['wwan']['currentNWserviceType']
Expand Down
32 changes: 32 additions & 0 deletions examples/auto_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3

"""Example file for eternalegypt library."""

import sys
import asyncio
import aiohttp
import logging

import eternalegypt

async def set_autoconnect_mode(mode):
"""Example of printing the current upstream."""
jar = aiohttp.CookieJar(unsafe=True)
websession = aiohttp.ClientSession(cookie_jar=jar)

try:
modem = eternalegypt.Modem(hostname=sys.argv[1], websession=websession)
await modem.login(password=sys.argv[2])

await modem.set_autoconnect_mode(mode)

await modem.logout()
except eternalegypt.Error:
print("Could not login")

await websession.close()

if len(sys.argv) != 4:
print("{}: <netgear ip> <netgear password> <mode>".format(sys.argv[0]))
else:
asyncio.get_event_loop().run_until_complete(set_autoconnect_mode(sys.argv[3]))
32 changes: 32 additions & 0 deletions examples/connect_lte.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3

"""Example file for eternalegypt library."""

import sys
import asyncio
import aiohttp
import logging

import eternalegypt

async def connect():
"""Example of printing the current upstream."""
jar = aiohttp.CookieJar(unsafe=True)
websession = aiohttp.ClientSession(cookie_jar=jar)

try:
modem = eternalegypt.Modem(hostname=sys.argv[1], websession=websession)
await modem.login(password=sys.argv[2])

await modem.connect_lte()

await modem.logout()
except eternalegypt.Error:
print("Could not login")

await websession.close()

if len(sys.argv) != 3:
print("{}: <netgear ip> <netgear password>".format(sys.argv[0]))
else:
asyncio.get_event_loop().run_until_complete(connect())
32 changes: 32 additions & 0 deletions examples/failover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3

"""Example file for eternalegypt library."""

import sys
import asyncio
import aiohttp
import logging

import eternalegypt

async def set_failover_mode(mode):
"""Example of printing the current upstream."""
jar = aiohttp.CookieJar(unsafe=True)
websession = aiohttp.ClientSession(cookie_jar=jar)

try:
modem = eternalegypt.Modem(hostname=sys.argv[1], websession=websession)
await modem.login(password=sys.argv[2])

await modem.set_failover_mode(mode)

await modem.logout()
except eternalegypt.Error:
print("Could not login")

await websession.close()

if len(sys.argv) != 4:
print("{}: <netgear ip> <netgear password> <mode>".format(sys.argv[0]))
else:
asyncio.get_event_loop().run_until_complete(set_failover_mode(sys.argv[3]))
3 changes: 2 additions & 1 deletion examples/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async def get_information():
result = await modem.information()
print("upstream: {}".format(result.upstream))
print("serial_number: {}".format(result.serial_number))
print("connection: {}".format(result.connection))
print("wire_connected: {}".format(result.wire_connected))
print("mobile_connected: {}".format(result.mobile_connected))
print("connection_text: {}".format(result.connection_text))
print("connection_type: {}".format(result.connection_type))
print("current_nw_service_type: {}".format(result.current_nw_service_type))
Expand Down

0 comments on commit 79fd521

Please sign in to comment.