-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-listener.py
executable file
·224 lines (185 loc) · 6.85 KB
/
server-listener.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
#!/usr/bin/env python
import socket
import os
import sys
import time
import netifaces as ni
#video on should be previous fps
video_on = 0
#dictionaries for processes
pid = {
"VIDEO_STREAM": False,
"DIRECTION_STREAM": False,
"GPS_STREAM": False,
"CAPTURE_PHOTO": False
}
command = {
"VIDEO_STREAM": "/home/pi/robo-ops/pi-clients/start_video_stream2.sh",
"DIRECTION_STREAM": "/home/pi/robo-ops/pi-clients/dof_device.py",
"GPS_STREAM": "/home/pi/robo-ops/pi-clients/start_gps.sh",
"CAPTURE_PHOTO": "/home/pi/robo-ops/pi-clients/take_pic.sh"
}
# host name and port number
host = "192.168.1.132"
port = 9000
pic_port = 7000
#log outputs
def writeToLog(the_string):
f = open("/home/pi/robo-ops/pi-clients/server_listener.log", "a")
f.write(the_string + "\n")
f.close()
#connect to server
def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
print("server-listener: connected to command server")
writeToLog("server-listener: connected to command server")
#find my ip
ni.ifaddresses('wlan0')
ip = ni.ifaddresses('wlan0')[2][0]['addr']
send(ip + "~", s)
return s
#send string to socket
def send(a_str, s):
s.sendall(a_str.encode())
#send a picture with given file name
def sendPic(file_name):
try:
f = open(file_name, 'r')
pic_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
pic_sock.connect((host, pic_port))
print("server-listener: sending picture to server ip: " + str(host) + "port: " + str(port))
writeToLog("server-listener: sending picture to server ip: " + str(host) + "port: " + str(port))
# send file
data = f.read(1024)
while (data):
pic_sock.sendall(data)
data = f.read(1024)
print("server-listener: done sending pic")
writeToLog("server-listener: done sending pic")
except:
print("server-listener: error in sendPic")
writeToLog("server-listener: error in sendPic")
finally:
f.close()
pic_sock.close()
#recieve data from socket
def receive(s):
print("server-listener: waiting to receive command")
writeToLog("server-listener: waiting to receive command")
command = ''
data = ' '
while (data != "|"):
data = s.recv(1).decode()
sys.stdout.write(data)
if (len(data) == 0):
break
if (data != " "):
command += data
print ("server-listener: " + command)
writeToLog("server-listener: " + command)
return command
#start process
def start_process(input_str):
try:
global video_on
process = input_str.split("|")
args = process[0].split(":")
if (len(args) < 2):
return
start_stop = args[0]
the_command = args[1]
#start process
if (start_stop == "START"):
if (pid[the_command] == False):
if (the_command == "CAPTURE_PHOTO" and pid["VIDEO_STREAM"] != False):
os.system("/home/pi/robo-ops/pi-clients/stop_video.sh")
print("stopped video")
writeToLog("stopped video")
time.sleep(2)
#return
process_args = [command[the_command]]
if (len(args) > 2):
for i in range (2, len(args)):
process_args.append(args[i])
#add host param if needed
if (the_command != "CAPTURE_PHOTO"):
process_args.append(str(host))
#if we need to set video variable
if (the_command == "VIDEO_STREAM"):
video_on = process_args[-2]
the_pid = os.fork()
#child
if the_pid == 0:
os.execl(command[the_command], *tuple(process_args))
assert False, 'server-listener: error starting child procces'
#parent
else:
if (the_command != "CAPTURE_PHOTO"):
pid[the_command] = the_pid
else:
if (video_on != 0):
time.sleep(1)
#child
if (os.fork() == 0):
os.execl(command["VIDEO_STREAM"], *(command["VIDEO_STREAM"], video_on, host))
#start_process("START:VIDEO_STREAM:" + str(video_on) + "|")
while(not os.path.isfile("/home/pi/robo-ops/pi-clients/pics/" + str(process_args[len(process_args) - 1]))):
continue
sendPic("/home/pi/robo-ops/pi-clients/pics/" + str(process_args[len(process_args) - 1]))
#stop process
elif (start_stop == "STOP"):
if (pid[the_command] != False):
if (the_command == "VIDEO_STREAM"):
#kill = "sudo pkill ffmpeg"
os.system("/home/pi/robo-ops/pi-clients/stop_video.sh")
video_on = 0
elif (the_command == "GPS_STREAM"):
os.system("/home/pi/robo-ops/pi-clients/stop_gps.sh")
else:
kill = "sudo kill -9 " + str(pid[the_command])
os.system(kill)
#os.system(kill)
pid[the_command] = False
except:
print("server-listener: error in start_process")
finally:
return
time.sleep(10)
if (len(sys.argv) < 2):
print("server-listener: server_ip")
writeToLog("server-listener: server_ip")
sys.exit(1)
host = str(sys.argv[1])
print("server-listener: starting with server ip: " + str(host) + " and port: " + str(port))
writeToLog("server-listener: starting with server ip: " + str(host) + " and port: " + str(port))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
isConnected = 0
while True:
while isConnected == 0:
try:
s = connect()
isConnected = 1
except socket.error:
#s.close()
isConnected = 0
while True:
try:
a_command = None
a_command = receive(s)
sys.stdout.flush()
if a_command != None and a_command != "":
start_process(a_command)
else:
print("server-listener: disconnected from command server")
writeToLog("server-listener: disconnected from command server")
#s.close()
isConnected = 0
break
except socket.error:
print("server-listener: disconnected from command server")
writeToLog("server-listener: disconnected from command server")
s.close()
isConnected = 0
break
s.close()