mirror of
https://github.com/NanjingForestryUniversity/supermachine-wood.git
synced 2025-11-08 18:23:54 +00:00
大概可以用的两个sock,且简单回复版本
This commit is contained in:
parent
389f841d3e
commit
c4b547750f
@ -10,34 +10,13 @@ import time
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from root_dir import ROOT_DIR
|
from root_dir import ROOT_DIR
|
||||||
from utils import PreSocket, receive_sock, parse_protocol, ack_sock, done_sock
|
from utils import PreSocket, receive_sock, parse_protocol, ack_sock, done_sock, DualSock, simple_sock
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
def try_connect(connect_ip: str, port_number: int, is_repeat: bool = False, max_reconnect_times: int = 50) -> (bool, socket.socket):
|
|
||||||
"""
|
|
||||||
尝试连接.
|
|
||||||
|
|
||||||
:param is_repeat: 是否是重新连接
|
|
||||||
:param max_reconnect_times:最大重连次数
|
|
||||||
:return: (连接状态True为成功, Socket / None)
|
|
||||||
"""
|
|
||||||
reconnect_time = 0
|
|
||||||
while reconnect_time < max_reconnect_times:
|
|
||||||
logging.warning(f'尝试{"重新" if is_repeat else ""}发起第{reconnect_time+1}次连接...')
|
|
||||||
try:
|
|
||||||
connected_sock = PreSocket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
connected_sock.connect((connect_ip, port_number))
|
|
||||||
except Exception as e:
|
|
||||||
reconnect_time += 1
|
|
||||||
logging.error(f'第{reconnect_time}次连接失败\n {e}')
|
|
||||||
continue
|
|
||||||
logging.warning(f'{"重新" if is_repeat else ""}连接成功')
|
|
||||||
return True, connected_sock
|
|
||||||
return False, None
|
|
||||||
|
|
||||||
|
|
||||||
def process_cmd(cmd: str, data: any, connected_sock: PreSocket, detector: WoodClass) -> bool:
|
def process_cmd(cmd: str, data: any, connected_sock: socket.socket, detector: WoodClass) -> bool:
|
||||||
"""
|
"""
|
||||||
处理指令
|
处理指令
|
||||||
|
|
||||||
@ -50,14 +29,14 @@ def process_cmd(cmd: str, data: any, connected_sock: PreSocket, detector: WoodCl
|
|||||||
|
|
||||||
if cmd == 'IM':
|
if cmd == 'IM':
|
||||||
wood_color = detector.predict(data)
|
wood_color = detector.predict(data)
|
||||||
response = done_sock(connected_sock, cmd_type=cmd, result=wood_color)
|
response = simple_sock(connected_sock, cmd_type=cmd, result=wood_color)
|
||||||
elif cmd == 'TR':
|
elif cmd == 'TR':
|
||||||
detector = WoodClass(w=4096, h=1200, n=3000, debug_mode=False)
|
detector = WoodClass(w=4096, h=1200, n=3000, debug_mode=False)
|
||||||
detector.fit_pictures(data_path=data)
|
detector.fit_pictures(data_path=data)
|
||||||
response = done_sock(connected_sock, cmd_type=cmd)
|
response = simple_sock(connected_sock, cmd_type=cmd)
|
||||||
elif cmd == 'MD':
|
elif cmd == 'MD':
|
||||||
detector.load(path=data)
|
detector.load(path=data)
|
||||||
response = done_sock(connected_sock, cmd_type=cmd)
|
response = simple_sock(connected_sock, cmd_type=cmd)
|
||||||
else:
|
else:
|
||||||
logging.error(f'错误指令,指令为{cmd}')
|
logging.error(f'错误指令,指令为{cmd}')
|
||||||
response = False
|
response = False
|
||||||
@ -71,29 +50,22 @@ def main(is_debug=False):
|
|||||||
console_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING)
|
console_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING)
|
||||||
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s',
|
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s',
|
||||||
handlers=[file_handler, console_handler], level=logging.DEBUG)
|
handlers=[file_handler, console_handler], level=logging.DEBUG)
|
||||||
|
dual_sock = DualSock()
|
||||||
|
|
||||||
received_status, received_sock = False, None
|
while not dual_sock.status:
|
||||||
send_status, send_sock = False, None
|
dual_sock.reconnect()
|
||||||
while not (received_status or send_status):
|
|
||||||
received_status, received_sock = try_connect(connect_ip='127.0.0.1', port_number=21122)
|
|
||||||
send_status, send_sock = try_connect(connect_ip='127.0.0.1', port_number=21123)
|
|
||||||
# socket_send = PreSocket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
# socket_send.connect(('127.0.0.1', 21123))
|
|
||||||
# a = b'\xaa\x00\x00\x00\x05\x20\x44\x54\x52\xff\xff\xff\xbb'
|
|
||||||
# received_sock.send(a)
|
|
||||||
model_path = os.path.join(ROOT_DIR, r"C:\Users\FEIJINTI\PycharmProjects\wood_color\models\model_2022-09-28_13-15.p")
|
model_path = os.path.join(ROOT_DIR, r"C:\Users\FEIJINTI\PycharmProjects\wood_color\models\model_2022-09-28_13-15.p")
|
||||||
detector = WoodClass(w=4096, h=1200, n=3000, debug_mode=False)
|
detector = WoodClass(w=4096, h=1200, n=3000, debug_mode=False)
|
||||||
detector.load(path=model_path)
|
detector.load(path=model_path)
|
||||||
while True:
|
while True:
|
||||||
pack, next_pack = receive_sock(received_sock)
|
pack, next_pack = receive_sock(dual_sock)
|
||||||
if pack == b"":
|
if pack == b"":
|
||||||
received_status, received_sock = try_connect(connect_ip='127.0.0.1', port_number=21122)
|
dual_sock.reconnect()
|
||||||
send_status, send_sock = try_connect(connect_ip='127.0.0.1', port_number=21123)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
cmd, data = parse_protocol(pack)
|
cmd, data = parse_protocol(pack)
|
||||||
# ack_sock(received_sock, cmd_type=cmd)
|
# ack_sock(received_sock, cmd_type=cmd)
|
||||||
process_cmd(cmd=cmd, data=data, connected_sock=send_sock, detector=detector)
|
process_cmd(cmd=cmd, data=data, connected_sock=dual_sock, detector=detector)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
84
utils.py
84
utils.py
@ -68,6 +68,30 @@ class Logger(object):
|
|||||||
print(content)
|
print(content)
|
||||||
|
|
||||||
|
|
||||||
|
def try_connect(connect_ip: str, port_number: int, is_repeat: bool = False, max_reconnect_times: int = 50) -> (
|
||||||
|
bool, socket.socket):
|
||||||
|
"""
|
||||||
|
尝试连接.
|
||||||
|
|
||||||
|
:param is_repeat: 是否是重新连接
|
||||||
|
:param max_reconnect_times:最大重连次数
|
||||||
|
:return: (连接状态True为成功, Socket / None)
|
||||||
|
"""
|
||||||
|
reconnect_time = 0
|
||||||
|
while reconnect_time < max_reconnect_times:
|
||||||
|
logging.warning(f'尝试{"重新" if is_repeat else ""}发起第{reconnect_time + 1}次连接...')
|
||||||
|
try:
|
||||||
|
connected_sock = PreSocket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
connected_sock.connect((connect_ip, port_number))
|
||||||
|
except Exception as e:
|
||||||
|
reconnect_time += 1
|
||||||
|
logging.error(f'第{reconnect_time}次连接失败\n {e}')
|
||||||
|
continue
|
||||||
|
logging.warning(f'{"重新" if is_repeat else ""}连接成功')
|
||||||
|
return True, connected_sock
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
|
||||||
class PreSocket(socket.socket):
|
class PreSocket(socket.socket):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
@ -88,7 +112,30 @@ class PreSocket(socket.socket):
|
|||||||
self.pre_pack = temp + pre_pack
|
self.pre_pack = temp + pre_pack
|
||||||
|
|
||||||
|
|
||||||
def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'', time_out: float = -1.0, time_out_single=0.5) -> (bytes, bytes):
|
class DualSock(PreSocket):
|
||||||
|
def __init__(self, connect_ip='127.0.0.1', recv_port:int = 21122, send_port: int = 21123):
|
||||||
|
super().__init__()
|
||||||
|
received_status, self.received_sock = try_connect(connect_ip=connect_ip, port_number=recv_port)
|
||||||
|
send_status, self.send_sock = try_connect(connect_ip=connect_ip, port_number=send_port)
|
||||||
|
self.status = received_status and send_status
|
||||||
|
|
||||||
|
def send(self, *args, **kwargs) -> int:
|
||||||
|
return self.send_sock.send(*args, **kwargs)
|
||||||
|
|
||||||
|
def receive(self, *args, **kwargs) -> bytes:
|
||||||
|
return self.received_sock.receive(*args, **kwargs)
|
||||||
|
|
||||||
|
def set_prepack(self, *args, **kwargs):
|
||||||
|
return self.received_sock.receive(*args, **kwargs)
|
||||||
|
|
||||||
|
def reconnect(self, connect_ip='127.0.0.1', recv_port:int = 21122, send_port: int = 21123):
|
||||||
|
received_status, self.received_sock = try_connect(connect_ip=connect_ip, port_number=recv_port)
|
||||||
|
send_status, self.send_sock = try_connect(connect_ip=connect_ip, port_number=send_port)
|
||||||
|
return received_status and send_status
|
||||||
|
|
||||||
|
|
||||||
|
def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'', time_out: float = -1.0, time_out_single=0.5) -> (
|
||||||
|
bytes, bytes):
|
||||||
"""
|
"""
|
||||||
从指定的socket中读取数据.
|
从指定的socket中读取数据.
|
||||||
|
|
||||||
@ -211,7 +258,7 @@ def parse_protocol(data: bytes) -> (str, any):
|
|||||||
return cmd, data
|
return cmd, data
|
||||||
|
|
||||||
|
|
||||||
def ack_sock(send_sock:PreSocket, cmd_type: str) -> bool:
|
def ack_sock(send_sock: socket.socket, cmd_type: str) -> bool:
|
||||||
'''
|
'''
|
||||||
发送应答
|
发送应答
|
||||||
:param cmd_type:指令类型
|
:param cmd_type:指令类型
|
||||||
@ -227,7 +274,7 @@ def ack_sock(send_sock:PreSocket, cmd_type: str) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def done_sock(send_sock: PreSocket, cmd_type: str, result: int = '') -> bool:
|
def done_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool:
|
||||||
'''
|
'''
|
||||||
发送任务完成指令
|
发送任务完成指令
|
||||||
:param cmd_type:指令类型
|
:param cmd_type:指令类型
|
||||||
@ -251,9 +298,40 @@ def done_sock(send_sock: PreSocket, cmd_type: str, result: int = '') -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool:
|
||||||
|
'''
|
||||||
|
发送任务完成指令
|
||||||
|
:param cmd_type:指令类型
|
||||||
|
:param send_sock:指定sock
|
||||||
|
:param result:数据
|
||||||
|
:return:是否发送成功
|
||||||
|
'''
|
||||||
|
cmd_type = cmd_type.strip().upper()
|
||||||
|
if cmd_type == 'IM':
|
||||||
|
if result == 0:
|
||||||
|
msg = b'Q'
|
||||||
|
elif result == 1:
|
||||||
|
msg = b'Z'
|
||||||
|
elif result == 2:
|
||||||
|
msg = b'S'
|
||||||
|
elif cmd_type == 'TR':
|
||||||
|
msg = b'A'
|
||||||
|
elif cmd_type == 'MD':
|
||||||
|
msg = b'D'
|
||||||
|
try:
|
||||||
|
send_sock.send(msg)
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f'发送完成指令失败,错误类型:{e}')
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
log = Logger(is_to_file=True)
|
log = Logger(is_to_file=True)
|
||||||
log.log("nihao")
|
log.log("nihao")
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
a = np.ones((100, 100, 3))
|
a = np.ones((100, 100, 3))
|
||||||
log.log(a.shape)
|
log.log(a.shape)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user