This commit is contained in:
Zhengyu Peng
2021-12-29 21:32:37 -05:00
parent 788f1e460c
commit 2292603d6d
2 changed files with 118 additions and 161 deletions

View File

@ -1,5 +1,30 @@
#!python
#
# 2021 Zhengyu Peng
# Website: https://zpeng.me
#
# ` `
# -:. -#:
# -//:. -###:
# -////:. -#####:
# -/:.://:. -###++##:
# .. `://:- -###+. :##:
# `:/+####+. :##:
# .::::::::/+###. :##:
# .////-----+##: `:###:
# `-//:. :##: `:###/.
# `-//:. :##:`:###/.
# `-//:+######/.
# `-/+####/.
# `+##+.
# :##:
# :##:
# :##:
# :##:
# :##:
# .+:
import socket
import errno
from threading import Thread
import json
@ -15,6 +40,8 @@ class TCPServer(Thread):
SIG_DISCONNECT = 2
def __init__(self, out_cmd_queue):
Thread.__init__(self)
self.cmd_queue = out_cmd_queue
with open('./config.json', 'r') as read_file:
@ -36,7 +63,6 @@ class TCPServer(Thread):
# self.status.emit(self.STOP, '')
pass
else:
# self.status.emit(self.LISTEN, '')
while True:
# Wait for a connection
# print('wait for a connection')
@ -55,38 +81,16 @@ class TCPServer(Thread):
try:
data = self.connection.recv(4096)
except socket.error as e:
err = e.args[0]
# if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
# if self.current_motion is not None:
# self.move(self.current_motion, 0.005)
# else:
# time.sleep(1)
# print('No data available')
# continue
# else:
# a "real" error occurred
print(e)
break
# sys.exit(1)
else:
if data:
self.cmd_queue.put(data.decode())
# move_routine(self, path, interval)
# self.message.emit(
# addr[0]+':'+str(addr[1]),
# data.decode())
else:
# self.status.emit(self.LISTEN, '')
self.cmd_queue.put('standby')
break
# elif self.signal == self.SIG_DISCONNECT:
# self.signal = self.SIG_NORMAL
# # self.connection.close()
# # self.status.emit(self.LISTEN, '')
# break
finally:
self.tcp_socket.close()
self.cmd_queue.put('standby')
# self.status.emit(self.STOP, '')
print('exit')