-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (74 loc) · 2.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import argparse
import os
import time
import threading
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
import cli
import netifaces
import pychromecast
htdocs = os.path.join(os.path.dirname(os.path.abspath(__file__)), "htdocs")
os.chdir(htdocs)
server = HTTPServer(("0.0.0.0", 45114), SimpleHTTPRequestHandler)
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
def server_up():
thread.start()
print("starting server on port {}".format(server.server_port))
def server_down():
server.shutdown()
print("stopping server on port {}".format(server.server_port))
def audio_play(chromecastip, filename):
"""Start playing music"""
url = "http://{}:{}/{}".format(
netifaces.ifaddresses("eth0")[netifaces.AF_INET][0]["addr"], 45114, filename
)
cast = pychromecast.Chromecast(chromecastip)
cast.connect()
cast.wait()
mc = cast.media_controller
mc.play_media(url, "audio/mp3")
time.sleep(5)
def check_qos_enabled(interface):
result = cli.execute("show policy-map interface {} output".format(interface))
if len(result) == 0:
return False
else:
return True
def config_qos(interface, policymap, desired):
if desired is False:
prefix = "no "
else:
prefix = ""
cli.configure(
[
"interface {}".format(interface),
"{}service-policy output {}".format(prefix, policymap),
"end",
]
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--chromecastip", help="IP Address of Chromecast", required=True
)
parser.add_argument(
"--interface", help="Interface name to enable QoS", required=True
)
parser.add_argument(
"--policymap",
help="Name of the policy map that is applied to the interface",
required=True,
)
args = parser.parse_args()
status = check_qos_enabled(args.interface)
server_up()
if status is True:
config_qos(args.interface, args.policymap, False)
audio_play(args.chromecastip, "OFF.mp3")
elif status is False:
config_qos(args.interface, args.policymap, True)
audio_play(args.chromecastip, "ON.mp3")
server_down()
if __name__ == "__main__":
main()