-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #393 from Stefal/lte_modem_support
Better modem state control
- Loading branch information
Showing
6 changed files
with
121 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#! /usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
import time | ||
import nmcli | ||
from sim_modem import * | ||
|
||
nmcli.disable_use_sudo() | ||
CONN_NAME='Cellular Modem' | ||
MODEM_PORT='/dev/ttymodemAT' | ||
USE_PUBLIC_IP=True | ||
def sleep(timeout, retry=10): | ||
def the_real_decorator(function): | ||
def wrapper(*args, **kwargs): | ||
retries = 0 | ||
while retries < retry: | ||
try: | ||
value = function(*args, **kwargs) | ||
if value is not None: | ||
return value | ||
except: | ||
print(f'{function.__name__}: Sleeping for {timeout + retries*timeout} seconds') | ||
time.sleep(timeout + retries*timeout) | ||
retries += 1 | ||
|
||
return wrapper | ||
return the_real_decorator | ||
|
||
@sleep(10) | ||
def check_modem(): | ||
nmcli.connection.show(CONN_NAME) | ||
if nmcli.connection.show(CONN_NAME).get("GENERAL.STATE") == 'activated': | ||
return True | ||
|
||
@sleep(10, retry=2) | ||
def check_network_registration(): | ||
try: | ||
modem = Modem(MODEM_PORT) | ||
network_reg = int(modem.get_network_registration_status().split(",")[1]) | ||
if network_reg == 1 or network_reg == 2 or network_reg == 5: | ||
return True | ||
else: | ||
return False | ||
except Exception as e: | ||
print(e) | ||
raise Exception | ||
finally: | ||
modem.close() | ||
|
||
@sleep(10) | ||
def get_public_ip_address(): | ||
try: | ||
modem = Modem(MODEM_PORT) | ||
public_ip = modem.get_ip_address() | ||
|
||
except Exception as e: | ||
print (e) | ||
raise Exception | ||
finally: | ||
modem.close() | ||
return public_ip | ||
|
||
@sleep(10) | ||
def get_in_use_ip_address(): | ||
return nmcli.connection.show(CONN_NAME)['IP4.ADDRESS[1]'].split('/')[0] | ||
|
||
@sleep(10) | ||
def ping(host): | ||
res = os.system("ping -c 4 " + host + ' >/dev/null') | ||
return res == 0 | ||
|
||
check_modem() | ||
network_reg = check_network_registration() | ||
ip_in_use = get_in_use_ip_address() | ||
public_ip = get_public_ip_address() | ||
ping_host = ping('caster.centipede.fr') or ping('pch.net') | ||
|
||
if ip_in_use == None or public_ip == None or network_reg == False or ping_host == False: | ||
print("Internal Ip address in use: ", ip_in_use) | ||
print("Modem public Ip address: ", public_ip) | ||
print("Ping caster.centipede.fr or pch.net", ping_host) | ||
print("Modem problem. Switching to airplane mode and back to normal") | ||
try: | ||
print("Connecting to modem...") | ||
modem = Modem(MODEM_PORT) | ||
print("Sending AT+CFUN=0") | ||
modem.custom_read_lines('AT+CFUN=0') | ||
time.sleep(20) | ||
print("Sending AT+CFUN=1") | ||
modem.custom_read_lines('AT+CFUN=1') | ||
except Exception as e: | ||
print(e) | ||
finally: | ||
modem.close() | ||
|
||
elif USE_PUBLIC_IP and ip_in_use != public_ip: | ||
try: | ||
print("Internal Ip address in use: ", ip_in_use) | ||
print("Modem public Ip address: ", public_ip) | ||
modem = Modem(MODEM_PORT) | ||
modem.set_usbnetip_mode(1) | ||
print("Request to switch to public IP address done!") | ||
print("It could take a few minutes to be active") | ||
except Exception as e: | ||
print(e) | ||
finally: | ||
print("closing modem connexion") | ||
modem.close() | ||
#else: | ||
# print("We are already using the public Ip") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
[Unit] | ||
Description=Switch Simcom modem to public ip address | ||
Description=Check Simcom modem ip address | ||
After=network-online.target | ||
Wants=network-online.target | ||
|
||
[Service] | ||
Type=forking | ||
Type=simple | ||
User={user} | ||
ExecStart={python_path} {script_path}/tools/switch_to_public_ip.py | ||
ExecStart={python_path} {script_path}/tools/modem_check.py | ||
Restart=on-failure | ||
RestartSec=30 | ||
ProtectHome=read-only | ||
ProtectSystem=strict | ||
|
||
[Install] | ||
WantedBy=multi-user.target | ||
WantedBy=multi-user.target |
File renamed without changes.