Last active
March 26, 2016 14:41
-
-
Save ckorn/4fbf6a9447ce2c8a27b5 to your computer and use it in GitHub Desktop.
Wifi Control
This file contains hidden or 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
#!/usr/bin/python | |
import subprocess | |
import time | |
import re | |
SINK_REMOTE_NAME = "Internes Audio Analog Stereo on korn@pc" | |
ETHERNET_INTERFACE = "enp9s0" | |
UPDATE_INTERVAL = 1 | |
def check_call(c): | |
print(c) | |
return subprocess.check_call(c) | |
def check_output(c): | |
print(c) | |
return subprocess.check_output(c) | |
def getPCSinkIndex(): | |
global SINK_REMOTE_NAME | |
t=check_output(["pacmd", "list-sinks"]) | |
lines=t.splitlines() | |
r=re.compile(".*index: (?P<idx>\d+).*") | |
idx=-1 | |
for line in lines: | |
s=str(line) | |
m=r.match(s) | |
if m: | |
idx=m.group("idx") | |
if s.find(SINK_REMOTE_NAME) > -1: | |
return idx | |
return -1 | |
def ethernetEnabled(f): | |
f.seek(0) | |
return (f.read()[0] == "1") | |
def switchWifi(enable): | |
if enable: | |
return check_call(["connmanctl", "enable", "wifi"]) | |
else: | |
return check_call(["connmanctl", "disable", "wifi"]) | |
def waitForOnline(): | |
waitOnline=True | |
while waitOnline: | |
p1=subprocess.Popen(["connmanctl", "state"], stdout=subprocess.PIPE) | |
p2=subprocess.Popen(["grep", "online"], stdin=p1.stdout, stdout=subprocess.PIPE) | |
p1.stdout.close() | |
output = str(p2.communicate()[0]) | |
if output.find("online") > -1: | |
waitOnline=False | |
else: | |
time.sleep(1) | |
def changeSink(sink): | |
print("Setting default sink to: %(sink)s"%(locals())) | |
check_call(["pacmd", "set-default-sink", str(sink)]) | |
p1=subprocess.Popen(["pacmd", "list-sink-inputs"], stdout=subprocess.PIPE) | |
p2=subprocess.Popen(["grep", "index"], stdin=p1.stdout, stdout=subprocess.PIPE) | |
p1.stdout.close() | |
output=p2.communicate()[0].splitlines() | |
r=re.compile("(?:.*)index: (?P<idx>\d+)(?:.*)") | |
for line in output: | |
m=r.match(str(line)) | |
if m: | |
idx=m.group("idx") | |
print("Moving input: %(idx)s to sink %(sink)s"%(locals())) | |
check_call(["pacmd", "move-sink-input", str(idx), str(sink)]) | |
def setSink(enable, disable): | |
if enable != -1: | |
sEnable=str(enable) | |
check_call(["pacmd", "set-sink-mute", sEnable, "false"]) | |
check_call(["pacmd", "set-default-sink", sEnable]) | |
changeSink(enable) | |
if disable != -1: | |
sDisable=str(disable) | |
check_call(["pacmd", "set-sink-mute", sDisable, "true"]) | |
def switchSinks(pcSink): | |
if pcSink != -1: | |
setSink(pcSink, 0) | |
else: | |
setSink(0, pcSink) | |
def getPCSinkWait(ethernet): | |
#if not ethernet: | |
# return -1 | |
waitForOnline() | |
i = 0 | |
pcSink=-1 | |
while i<5 and pcSink == -1: | |
pcSink = getPCSinkIndex() | |
if pcSink == -1: | |
time.sleep(1) | |
i+=1 | |
return pcSink | |
def main(): | |
global ETHERNET_INTERFACE, UPDATE_INTERVAL | |
f=open('/sys/class/net/'+ETHERNET_INTERFACE+'/carrier', 'r') | |
first=True | |
ethernet=False | |
pcSink=-1 | |
# wait until everything is loaded | |
time.sleep(10) | |
while True: | |
if first: | |
first=False | |
ethernet = ethernetEnabled(f) | |
switchWifi(not ethernet) | |
pcSink=getPCSinkWait(ethernet) | |
switchSinks(pcSink) | |
else: | |
newEthernet = ethernetEnabled(f) | |
if newEthernet != ethernet: | |
ethernet = newEthernet | |
switchWifi(not ethernet) | |
newPcSink=getPCSinkWait(ethernet) | |
if newPcSink != pcSink: | |
pcSink=newPcSink | |
switchSinks(pcSink) | |
print("ethernet=%(ethernet)s"%(locals())) | |
print("pcSink=%(pcSink)s"%(locals())) | |
time.sleep(UPDATE_INTERVAL) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment