Skip to content

Instantly share code, notes, and snippets.

@Self-Perfection
Last active October 26, 2021 08:39
Show Gist options
  • Save Self-Perfection/23a99d2a0ffa117e0085c5b241f131a2 to your computer and use it in GitHub Desktop.
Save Self-Perfection/23a99d2a0ffa117e0085c5b241f131a2 to your computer and use it in GitHub Desktop.
Lame script to toggle airplane mode on/off on SailfishOS if network connection breaks (see https://4pda.ru/forum/index.php?s=&showtopic=501263&view=findpost&p=64055550 )
#!/usr/bin/python
import calendar
import dbus
import signal
import subprocess
import time
tool_name = 'MTS_reconnector'
start_time = calendar.timegm(time.gmtime())
def set_radio_mode(enabled=True):
bus = dbus.SystemBus()
object = bus.get_object('com.nokia.mce','/com/nokia/mce/request')
interface = dbus.Interface(object,'com.nokia.mce.request')
interface.req_radio_states_change(enabled, 1)
def send_notif(title='', body='', preview_body='', preview_summary='',
replaces_id=0, expire_timeout=0):
bus = dbus.SessionBus()
object = bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications')
interface = dbus.Interface(object,'org.freedesktop.Notifications')
notif_id = interface.Notify(tool_name,
replaces_id,
"icon-m-notifications",
title,
body,
dbus.Array(["default", ""]),
dbus.Dictionary({"x-nemo-preview-body": preview_body,
"x-nemo-preview-summary": preview_summary},
signature='sv'),
#Looks like nonzero expire_timeout is accounted as True and makes notification disappear after 2-3s
expire_timeout)
# Run no more than an hour
while calendar.timegm(time.gmtime()) - start_time < 3600:
exit_code = subprocess.call(['ip', 'route', 'get', '8.8.8.8'])
if exit_code > 0:
print('Route to test host is not present. Probably network is not connected yet.')
time.sleep(10)
continue
exit_code = subprocess.call(['ping', '-w10', '8.8.8.8'])
if exit_code > 0:
msg = 'Network failure. Reconnecting...'
send_notif( preview_summary=msg)
subprocess.call(['logger', '-t', tool_name, msg])
# Probably instead of going to offline mode it is enough to `systemctl restart connman.service`
# https://github.com/sailfishos/sailfish-utilities/blob/master/tools/restart_network.sh
set_radio_mode(False)
time.sleep(2)
set_radio_mode(True)
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment