-
Notifications
You must be signed in to change notification settings - Fork 0
/
localserver.py
84 lines (64 loc) · 2.56 KB
/
localserver.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
import asyncio
import os
from flask import Flask, request
from icecream.icecream import IceCreamDebugger
from regex import R
import subprocess
app = Flask(__name__)
ic = IceCreamDebugger()
working_dir = os.getcwd()
async def run_script(script: str):
proc = await asyncio.create_subprocess_shell(
script,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
return stdout, stderr
@app.route('/api/set-wifi-credentials', methods=['POST'])
def set_wifi_credentials():
try:
wifi_ssid = request.json.get('ssid')
wifi_password = request.json.get('password')
ic(f"Received {wifi_password}, {wifi_ssid}")
network_block = """
network={
ssid=wifi_ssid
psk=wifi_psk
key_mgmt=WPA-PSK
}
"""
network_block = network_block.replace("wifi_ssid", f"\"{wifi_ssid}\"").replace("wifi_psk", f"\"{wifi_password}\"").strip()
# remove everything below the 3rd line
os.system("sudo sed -i '4,$d' /etc/wpa_supplicant/wpa_supplicant.conf")
# with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'a') as file:
# network_block = network_block.replace("Your_SSID", wifi_ssid).replace("Your_PSK_Password", wifi_password)
os.system(f"sudo echo '{network_block}' >> /etc/wpa_supplicant/wpa_supplicant.conf")
print("J3 Server Online")
return f'Credentials updated successfully with {wifi_ssid} and {wifi_password}', 200
except Exception as e:
ic(str(e))
return str(e), 500
@app.route('/api/ping', methods=['GET'])
def ping_connection():
ic("J3 Localserver pinged!")
return f"Product is Online", 200
@app.route('/api/turn_off_hotspot', methods=['GET'])
async def turn_off_hotspot():
stdout, stderr = await run_script("sudo ~/LL-MAI-PI-SOFTWARE/scripts/setup/scrap_access_point.sh")
ic(stdout, stderr)
return "Turned Off Hotspot", 200
@app.route('/api/register_profile', methods=['POST'])
async def register_profile():
ic("Registering Profile")
uuid = request.json.get('uuid')
age = request.json.get('age')
return "registerd id", 200
@app.route('/api/restart', methods=['GET'])
def restart():
ic("Received signal for restart")
subprocess.call(["sudo", "/home/rishi/falcon_mini/scripts/setup/scrap_access_point.sh"])
# os.system("sudo ~/LL-MAI-PI-SOFTWARE/scripts/setup/scrap_access_point.sh")
return "Restarted System", 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)