-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
258 lines (229 loc) · 7.91 KB
/
app.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Made with <3 By Albony
from flask import Flask, request, render_template, jsonify, Response
import time
import subprocess
#from flask_cors import CORS # For Graph Plotting (test)
app = Flask(__name__)
#CORS(app)
#def get_bandwidth_usage():
# output = subprocess.check_output(['bash', 'scripts/get_bandwidth_usage.sh'])
# return output.decode('utf-8')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'button1' in request.form:
subprocess.run(['bash', 'scripts/WLAN_ON.sh'])
return '''
<script>alert("WiFi Turned On Successfully")</script>
<script>window.location.replace("/");</script>
'''
elif 'button2' in request.form:
subprocess.run(['bash', 'scripts/WLAN_OFF.sh'])
return '''
<script>alert("WiFi Turned Off Successfully")</script>
<script>window.location.replace("/");</script>
'''
elif 'button3' in request.form:
subprocess.run(['bash', 'scripts/NEW_IP.sh'])
return '''
<script>alert("Done")</script>
<script>window.location.replace("/");</script>
'''
else:
return '''
<!DOCTYPE html>
<html>
<head>
<title>WiFi Control Panel</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
padding: 0;
font-family: 'Arial', sans-serif;
background-color: #1e1e1e;
color: #d9d9d9;
}
#container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #292929;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
#header {
text-align: center;
margin-bottom: 20px;
}
h1 {
font-size: 32px;
margin-top: 0;
margin-bottom: 0;
color: #d9d9d9;
text-shadow: 2px 2px #333;
}
p {
font-size: 15px;
line-height: 1.5;
margin: 25px 0;
text-align: center;
}
.form-container {
text-align: center;
}
button {
background-color: #4d4d4d;
color: #d9d9d9;
padding: 10px 20px;
border-radius: 5px;
border: none;
font-size: 16px;
margin: 10px;
cursor: pointer;
box-shadow: 2px 2px #333;
}
button:hover {
background-color: #666666;
color: #d9d9d9;
box-shadow: 2px 2px #333;
}
button:active {
background-color: #666666;
color: #d9d9d9;
box-shadow: none;
transform: translateY(2px);
}
#bandwidth-usage {
margin-top: 20px;
text-align: center;
}
#bandwidth-usage p {
font-size: 16px;
margin: 5px;
}
#temperature {
font-size: 18px;
margin-top: 20px;
text-align: center;
color: #d9d9d9;
text-shadow: 2px 2px #333;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>ONT Control Panel</h1>
</div>
<div id="temperature">ONT Temperature: <span id="temp-value">--</span></div>
<div id="temperature">
<!-- <div id="temperature"><span id="power-value">--</span></div> --!>
<div id="temperature">Uptime: <span id="uptime-value">--</span></div>
</div>
<div id="temperature">
Inbound bandwidth: <span id="in-speed">0 Mb/s</span>
</div>
<div id="temperature">
Outbound bandwidth: <span id="out-speed">0 Mb/s</span>
</div>
<br> </br>
<div class="form-container">
<form method="post">
<button type="submit" name="button1">Turn WiFi ON</button>
<button type="submit" name="button2">Turn WiFi OFF</button>
<button type="submit" name="button3">Get a New IP</button>
</form>
</div>
<p> Made with <3 By Albony </p>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function updateTemperature() {
fetch('/temperature') // call the '/temperature' endpoint to get the temperature
.then(response => response.text()) // parse the response as text
.then(temperature => {
// update the temperature value in the UI
document.querySelector('#temp-value').textContent = temperature;
})
.catch(error => {
console.error('Failed to update temperature:', error);
});
}
// update the temperature every 5 seconds
setInterval(updateTemperature, 5000);
function updatePower() {
fetch('/optical')
.then(response => response.text())
.then(optical => {
document.querySelector('#power-value').textContent = optical;
})
.catch(error => {
console.error('Failed to update power:', error);
});
}
setInterval(updatePower, 5000);
function updateUptime() {
fetch('/uptime')
.then(response => response.text())
.then(uptime => {
document.querySelector('#uptime-value').textContent = uptime;
})
.catch(error => {
console.error('Failed to update uptime:', error);
});
}
setInterval(updateUptime, 5000);
const evtSource = new EventSource("/get_bandwidth");
evtSource.onmessage = function(event) {
const data = JSON.parse(event.data);
document.getElementById("in-speed").innerHTML = data.megabits_in_sec + " Mb/s";
document.getElementById("out-speed").innerHTML = data.megabits_out_sec + " Mb/s";
}
</script>
</body>
</html>
'''
@app.route('/temperature')
def temperature():
output = subprocess.check_output(['bash', 'scripts/get_temperature.sh'])
lines = output.decode('utf-8').split('\n')
temperature = lines[-2] # get the second to last line (which should be the temperature value)
return f"{temperature} \u2103"
@app.route('/uptime')
def uptime():
output = subprocess.check_output(['bash' , 'scripts/get_uptime.sh'])
lines = output.decode('utf-8').split('\n')
uptime = lines[-2]
return f"{uptime}"
@app.route('/optical')
def optical():
# get the output of optical.sh
output = subprocess.check_output(['bash', 'scripts/optical.sh']).decode('utf-8')
# extract the relevant information from the output
ic_temperature = output.split('\n')[1].split(':')[1]
bosa_temperature = output.split('\n')[2].split(':')[1]
vcc = output.split('\n')[3].split(':')[1]
txbias = output.split('\n')[4].split(':')[1]
txmod = output.split('\n')[5].split(':')[1]
txpower = output.split('\n')[6].split(':')[1].split()[1].replace('dBm', '')
rxpower = output.split('\n')[7].split(':')[1].split()[1].replace('dBm', '')
@app.route('/get_bandwidth')
def get_bandwidth():
def run_script():
process = subprocess.Popen(['bash', 'scripts/get_bandwidth.sh'], stdout=subprocess.PIPE)
while True:
output = process.stdout.readline().decode().strip()
if output == '' and process.poll() is not None:
break
yield 'data: {}\n\n'.format(output)
time.sleep(0.1)
return Response(run_script(), mimetype='text/event-stream')
#@app.route('/speed')
#def speedtest():
# result = subprocess.run('scripts/speedtest', capture_output=True, text=True)
# output = result.stdout.strip()
# return output
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=False)