-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireless_handler.py
123 lines (107 loc) · 3.95 KB
/
wireless_handler.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
from bluetooth import *
import subprocess
import os
import signal
from time import sleep
def send_file(s,dir):
size = os.path.getsize(dir)
file = open(dir,'rb')
#Send filename
s.send(dir)
s.send('\n')
packet = 1
#Send file in 1KB-parts, maximum for bluetooth.send()
while(packet):
packet = file.read(1024)
try:
s.send(packet)
except BluetoothError:
#Exit if client disconnect
packet = None
print("Disconnected")
file.close()
def is_connected(s):
try:
#If client connected, this does not throw error
s.getpeername()
return True
except:
return False
#Create blutoothsocket with RFCOMM protocol
pisocket = BluetoothSocket( RFCOMM )
#Bind socket to any port
pisocket.bind(("", PORT_ANY))
#Listen for 1 client
pisocket.listen(10)
#Instantiate webapp
webapp = None
#webapp = subprocess.Popen("sudo python3 /home/pi/citisense/webappl.py", shell=True, stdout=subprocess.PIPE, preexec_fn=os.setsid)
#Start with wifi-ap off
wifipower = False
subprocess.call("sudo service hostapd stop",shell=True)
sleep(1)
subprocess.call("sudo ifconfig wlan0 down",shell=True)
def recieve(sock,cnt):
data = 10 # ASCII \n
ret = bytearray()
try:
#Wait for command as long as client is connected
while(is_connected(client_socket) and cnt>0):
data = client_socket.recv(1)
if data[0] != 10:
ret.extend(data)
cnt = cnt - 1
except BluetoothError:
print("Client disconnected")
return None
return ret.decode("ASCII")
while(1):
#Accept connection, get client socket
client_socket,adr = pisocket.accept()
while(is_connected(client_socket)):
description = recieve(client_socket,1)
print(str(description))
if description == 'S': #ASCII S
# 'S' means send data logs
send_file(client_socket, "/home/pi/citisense/logs/data_log.csv")
print("Done")
elif description == 'P':
# 'P' means send picture
#send_file(client_socket, "/home/pi/citisense/logs/pic.jpg")
subprocess.call("sudo reboot",shell=True)
print("Done")
elif description == 'D' : #Ascii D
year = recieve(client_socket,2)
month = recieve(client_socket,2)
day = recieve(client_socket,2)
hour = recieve(client_socket,2)
minute = recieve(client_socket,2)
subprocess.call('sudo date +%Y%m%d -s "20'+str(year)+str(month)+str(day)+'"',shell=True)
subprocess.call('sudo date +%T -s "'+str(hour)+':'+str(minute)+':00"',shell=True)
elif wifipower and description == 'w': #ASCII w
# w means turn off wifi and webserver
os.killpg(os.getpgid(webapp.pid), signal.SIGTERM)
subprocess.call("sudo service hostapd stop",shell=True)
sleep(1)
subprocess.call("sudo ifconfig wlan0 down",shell=True)
wifipower = False
elif not wifipower and description == 'W': #ASCII W
# W means start wifi and webserver
subprocess.call("sudo ifconfig wlan0 up",shell=True)
subprocess.call("sudo service hostapd start",shell=True)
sleep(4)
subprocess.call("sudo service hostapd restart",shell=True)
sleep(2)
subprocess.call("sudo service hostapd restart",shell=True)
webapp = subprocess.Popen("sudo python3 /home/pi/citisense/webappl.py", shell=True, stdout=subprocess.PIPE, preexec_fn=os.setsid)
wifipower = True
sleep(1)
subprocess.call("sudo service xrdp restart",shell=True)
else:
#Unrecognized command
try:
client_socket.send("E\n")
except BluetoothError:
print("Disconnected")
client_socket.close()
pisocket.close()