-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
154 lines (119 loc) · 4.16 KB
/
server.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
from multiprocessing import Queue
import time
import socket, threading
import turtle
IP='172.20.5.205'
PORT=35355
turtle.setup(800, 600)
t=turtle.Turtle()
t.hideturtle()
t.penup()
t.setposition(-330, 0)
###executer
class Executer:
def __init__(self, tcpServer):
self.andRaspTCP = tcpServer
def startCommand(self, command):
if command=='refresh\n':
self.andRaspTCP.sendAll(format(50-(int(time.time()-atime)//60), '>2')+' '*32+format(50-(int(time.time()-btime)//60), '>2')+'\n')
elif command=='1\n':
self.andRaspTCP.sendAll(format(50-(int(time.time()-atime)//60), '>2')+'\n')
elif command=='2\n':
self.andRaspTCP.sendAll(format(50-(int(time.time()-btime)//60), '>2')+'\n')
else:
self.andRaspTCP.sendAll('er\n')
###tcpserverthread
class TCPServerThread(threading.Thread):
def __init__(self, commandQueue, tcpServerThreads, connections, connection, clientAddress):
threading.Thread.__init__(self)
self.commandQueue = commandQueue
self.tcpServerThreads = tcpServerThreads
self.connections = connections
self.connection = connection
self.clientAddress = clientAddress
def run(self):
try:
while True:
data = self.connection.recv(1024).decode()
# when break connection
if not data:
print ('tcp server :: exit :',self.connection)
break
print ('tcp server :: client :', data)
self.commandQueue.put(data)
except:
self.connections.remove(self.connection)
self.tcpServerThreads.remove(self)
exit(0)
self.connections.remove(self.connection)
self.tcpServerThreads.remove(self)
def send(self, message):
print ('tcp server :: ',message)
try:
for i in range(len(self.connections)):
self.connections[i].sendall(message.encode())
except:
pass
###tcpserver
class TCPServer(threading.Thread):
def __init__(self, commandQueue, HOST, PORT):
threading.Thread.__init__(self)
self.commandQueue = commandQueue
self.HOST = HOST
self.PORT = PORT
self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serverSocket.bind((self.HOST, self.PORT))
self.serverSocket.listen(1)
self.connections = []
self.tcpServerThreads = []
def run(self):
try:
while True:
print ('tcp server :: server wait...')
connection, clientAddress = self.serverSocket.accept()
self.connections.append(connection)
print ("tcp server :: connect :", clientAddress)
subThread = TCPServerThread(self.commandQueue, self.tcpServerThreads, self.connections, connection, clientAddress)
subThread.start()
self.tcpServerThreads.append(subThread)
except:
print ("tcp server :: serverThread error")
def sendAll(self, message):
try:
self.tcpServerThreads[0].send(message)
except:
pass
###main
atime, btime=time.time(), time.time()
##RASP
def rasp():
global atime, btime
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((IP, PORT))
while True:
data=sock.recv(200).decode()
if data=='A':
atime=time.time()
t.clear()
t.write('1번 세탁기 태그됨', font=('Time New Romans', 50, 'bold'))
elif data=='B':
btime=time.time()
t.clear()
t.write('2번 세탁기 태그됨', font=('Time New Romans', 50, 'bold'))
time.sleep(1)
t=threading.Thread(target=rasp, args=())
t.start()
##APP
# make public queue
commandQueue = Queue()
# init module
andRaspTCP = TCPServer(commandQueue, "", PORT)
andRaspTCP.start()
# set module to executer
commandExecuter = Executer(andRaspTCP)
while True:
try:
command = commandQueue.get()
commandExecuter.startCommand(command)
except:
pass