forked from eragefe/pi3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
140 lines (114 loc) · 4.54 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
from flask import Flask, render_template, request
import subprocess
import os
import time
app = Flask(__name__)
app.debug = True
@app.route('/', methods = ['GET', 'POST'])
def index():
return render_template('app.html')
@app.route('/wifi')
def wifi():
wifi_ap_array = scan_wifi_networks()
return render_template('wifi.html', wifi_ap_array = wifi_ap_array)
@app.route('/manual_ssid_entry')
def manual_ssid_entry():
return render_template('manual_ssid_entry.html')
@app.route('/tidal')
def tidal():
return render_template('tidal.html')
@app.route('/tidal_save_credentials', methods = ['GET', 'POST'])
def tidal_save_credentials():
ssid = request.form['ssid']
wifi_key = request.form['wifi_key']
create_upmpdcli(ssid, wifi_key)
os.system('mv upmpdcli.tmp /ro/etc/upmpdcli.conf')
os.system('cp /ro/etc/upmpdcli.conf /etc/upmpdcli.conf')
time.sleep(1)
os.system('service upmpdcli restart')
return render_template('save_credentials.html')
@app.route('/save_credentials', methods = ['GET', 'POST'])
def save_credentials():
ssid = request.form['ssid']
wifi_key = request.form['wifi_key']
create_wpa_supplicant(ssid, wifi_key)
os.system('mv wpa_supplicant.conf.tmp /ro/etc/wpa_supplicant/wpa_supplicant.conf')
os.system('cp /ro/etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf')
time.sleep(1)
os.system('/etc/init.d/networking restart')
os.system('wpa_cli -i wlan0 reconfigure')
return render_template('save_credentials.html')
@app.route('/dispon', methods = ['GET', 'POST'])
def dispon():
os.system('systemctl start oled2')
return render_template('app.html')
@app.route('/dispoff', methods = ['GET', 'POST'])
def dispoff():
os.system('systemctl stop oled')
os.system('systemctl stop oled2')
os.system('python /root/pi3/oled/off.py')
return render_template('app.html')
@app.route('/reboot', methods = ['GET', 'POST'])
def reboot():
time.sleep(1)
os.system('reboot')
@app.route('/poweroff', methods = ['GET', 'POST'])
def poweroff():
time.sleep(1)
os.system('poweroff')
@app.route('/squeeze', methods = ['GET', 'POST'])
def squeeze():
os.system('mount -o remount rw /ro')
os.system('cp /ro/etc/squeezelite /ro/etc/init.d')
os.system('squeezelite -n GDis_squeeze -o hw:0 -z')
os.system('cp /ro/root/pi3/templates/app_sq.html /ro/root/pi3/templates/app.html')
os.system('cp /root/pi3/templates/app_sq.html /root/pi3/templates/app.html')
os.system('mount -o remount ro /ro')
return render_template('app.html')
@app.route('/upnp', methods = ['GET', 'POST'])
def upnp():
os.system('mount -o remount rw /ro')
os.system('killall squeezelite')
os.system('rm /ro/etc/init.d/squeezelite')
os.system('cp /ro/root/pi3/templates/app_up.html /ro/root/pi3/templates/app.html')
os.system('cp /root/pi3/templates/app_up.html /root/pi3/templates/app.html')
os.system('mount -o remount ro /ro')
return render_template('app.html')
######## FUNCTIONS ##########
def scan_wifi_networks():
iwlist_raw = subprocess.Popen(['iwlist', 'scan'], stdout=subprocess.PIPE)
ap_list, err = iwlist_raw.communicate()
ap_array = []
for line in ap_list.decode('utf-8').rsplit('\n'):
if 'ESSID' in line:
ap_ssid = line[27:-1]
if ap_ssid != '':
ap_array.append(ap_ssid)
return ap_array
def create_wpa_supplicant(ssid, wifi_key):
temp_conf_file = open('wpa_supplicant.conf.tmp', 'w')
temp_conf_file.write('ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n')
temp_conf_file.write('update_config=1\n')
temp_conf_file.write('\n')
temp_conf_file.write('network={\n')
temp_conf_file.write(' ssid="' + ssid + '"\n')
if wifi_key == '':
temp_conf_file.write(' key_mgmt=NONE\n')
else:
temp_conf_file.write(' psk="' + wifi_key + '"\n')
temp_conf_file.write(' }')
temp_conf_file.close
os.system('mount -o remount rw /ro')
def create_upmpdcli(ssid, wifi_key):
temp_conf_file = open('upmpdcli.tmp', 'w')
temp_conf_file.write('uprclautostart = 1\n')
temp_conf_file.write('friendlyname = GDis-NGNP\n')
temp_conf_file.write('iconpath = /boot/gdis_upnp.png\n')
temp_conf_file.write('msfriendlyname = GDis-Tidal-server\n')
temp_conf_file.write('tidaluser = ' + ssid + '\n')
temp_conf_file.write('tidalpass = ' + wifi_key + '\n')
temp_conf_file.write('tidalquality = lossless\n')
temp_conf_file.close
os.system('mount -o remount rw /ro')
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 80)