-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgo.py
103 lines (88 loc) · 2.9 KB
/
algo.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
import errno
import socket
import sys
import time
import traceback
from config import *
class Algo:
host = WIFI_IP
port = WIFI_PORT
def __init__(self, host=WIFI_IP, port=WIFI_PORT):
self.host = host
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Socket Established")
try:
self.socket.bind(('', self.port))
except socket.error as e:
print("Bind failed", e)
sys.exit()
print("Bind completed")
self.socket.listen(3)
print("Waiting for connection from Algo...")
self.client_sock, self.address = self.socket.accept()
print("Connected to Algo @ " + str(self.address) + "!")
# receive the first message from client, know the client address
# print "Algo Connected"
def disconnect(self):
try:
self.socket.close()
except Exception as e:
print("Algo disconnection exception: %s" % str(e))
def write(self, msg):
try:
self.client_sock.sendto(msg.encode('utf-8'), self.address)
except socket.error as e:
if isinstance(e.args, tuple):
print("errno is %d" % e[0])
if e[0] == errno.EPIPE:
# remote peer disconnected
print("Detected remote disconnect")
else:
# for another error
pass
else:
print("socket error ", e)
sys.exit()
except IOError as e:
print("Algo read exception", e)
print(traceback.format_exc())
pass
def read(self):
try:
msg = self.client_sock.recv(1024).decode()
return msg
except socket.error as e:
if isinstance(e.args, tuple):
print("errno is %d" % e[0])
if e[0] == errno.EPIPE:
# remote peer disconnected
print("Detected remote disconnect")
else:
# for another error
pass
else:
print("socket error ", e)
sys.exit()
except IOError as e:
print("Algo read exception: ", e)
print(traceback.format_exc())
pass
if __name__ == '__main__':
algo = Algo()
try:
number = 8
counter = 1
while True:
time.sleep(5)
if counter == number:
algo.write("S" + str(counter)) # signal to stop
print(algo.read())
break
else:
algo.write("R" + str(counter)) # signal to take picture
print(algo.read())
counter += 1
except KeyboardInterrupt:
print("Terminating the program now...")