add more log
This commit is contained in:
@@ -33,11 +33,15 @@ COMMAND_MAPPINGS = {command_mappings}
|
|||||||
def process_command(buf):
|
def process_command(buf):
|
||||||
"""处理接收到的命令,返回对应的操作指令"""
|
"""处理接收到的命令,返回对应的操作指令"""
|
||||||
buf = buf.strip()
|
buf = buf.strip()
|
||||||
|
logging.debug("process_command: 原始输入='%s', 处理后='%s'" % (repr(buf), buf))
|
||||||
# 状态查询或心跳检测,返回特殊标记
|
# 状态查询或心跳检测,返回特殊标记
|
||||||
if buf == "state=?" or buf == "":
|
if buf == "state=?" or buf == "":
|
||||||
|
logging.debug("process_command: 匹配到状态查询/心跳检测")
|
||||||
return "status_check"
|
return "status_check"
|
||||||
if buf in COMMAND_MAPPINGS:
|
if buf in COMMAND_MAPPINGS:
|
||||||
|
logging.debug("process_command: 匹配到指令映射 '%s' -> '%s'" % (buf, COMMAND_MAPPINGS[buf]))
|
||||||
return COMMAND_MAPPINGS[buf]
|
return COMMAND_MAPPINGS[buf]
|
||||||
|
logging.debug("process_command: 未知指令 '%s'" % buf)
|
||||||
return "nodata"
|
return "nodata"
|
||||||
|
|
||||||
|
|
||||||
@@ -57,10 +61,15 @@ def main():
|
|||||||
print(f"[{{DEVICE_ID}}] 服务启动,监听 {{TMS_SERVER_IP}}:{{TMS_PORT}}")
|
print(f"[{{DEVICE_ID}}] 服务启动,监听 {{TMS_SERVER_IP}}:{{TMS_PORT}}")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
logging.debug("等待客户端连接...")
|
||||||
connection, address = sock.accept()
|
connection, address = sock.accept()
|
||||||
|
logging.info("客户端连接建立: %s:%s" % (address[0], address[1]))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
connection.settimeout(5)
|
connection.settimeout(5)
|
||||||
|
logging.debug("设置超时 5 秒,等待接收数据...")
|
||||||
buf_bytes = connection.recv(BUFSIZE)
|
buf_bytes = connection.recv(BUFSIZE)
|
||||||
|
logging.debug("收到原始数据: %s (长度=%d)" % (repr(buf_bytes), len(buf_bytes)))
|
||||||
buf = buf_bytes.decode('utf-8').strip()
|
buf = buf_bytes.decode('utf-8').strip()
|
||||||
|
|
||||||
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
||||||
@@ -70,18 +79,24 @@ def main():
|
|||||||
print(logstr)
|
print(logstr)
|
||||||
|
|
||||||
operation = process_command(buf)
|
operation = process_command(buf)
|
||||||
|
logging.debug("process_command 返回: '%s'" % operation)
|
||||||
|
|
||||||
if operation == "status_check":
|
if operation == "status_check":
|
||||||
# 状态查询或心跳检测,直接回复 ok
|
# 状态查询或心跳检测,直接回复 ok
|
||||||
logstr = "===" + datetime_str + " 状态查询/心跳检测,回复 ok"
|
response = 'ok'
|
||||||
|
logstr = "===" + datetime_str + " 状态查询/心跳检测,回复: " + response
|
||||||
logging.info(logstr)
|
logging.info(logstr)
|
||||||
print(logstr)
|
print(logstr)
|
||||||
connection.send(b'ok')
|
logging.debug("发送响应: '%s'" % response)
|
||||||
|
connection.send(response.encode('utf-8'))
|
||||||
|
logging.debug("响应发送完成")
|
||||||
elif operation == "nodata":
|
elif operation == "nodata":
|
||||||
|
response = 'Unknown command: %s' % buf
|
||||||
logstr = "===" + datetime_str + " 未知指令: '" + str(buf) + "'"
|
logstr = "===" + datetime_str + " 未知指令: '" + str(buf) + "'"
|
||||||
logging.warning(logstr)
|
logging.warning(logstr)
|
||||||
print(logstr)
|
print(logstr)
|
||||||
connection.send(('Unknown command: %s' % buf).encode('utf-8'))
|
logging.debug("发送未知指令响应: '%s'" % response)
|
||||||
|
connection.send(response.encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
logstr = "===" + datetime_str + " 映射到内部指令: '" + str(operation) + "'"
|
logstr = "===" + datetime_str + " 映射到内部指令: '" + str(operation) + "'"
|
||||||
logging.info(logstr)
|
logging.info(logstr)
|
||||||
@@ -91,8 +106,15 @@ def main():
|
|||||||
str(UPC_DEV_IP) + " " + str(UPC_DEV_PORT) + " " + \
|
str(UPC_DEV_IP) + " " + str(UPC_DEV_PORT) + " " + \
|
||||||
str(operation) + " " + str(LOG_FILE)
|
str(operation) + " " + str(LOG_FILE)
|
||||||
logging.info("执行外部命令: %s" % os_command)
|
logging.info("执行外部命令: %s" % os_command)
|
||||||
os.system(os_command)
|
logging.debug("开始执行外部命令...")
|
||||||
connection.send(('Command %s processed as %s' % (buf, operation)).encode('utf-8'))
|
exit_code = os.system(os_command)
|
||||||
|
logging.debug("外部命令执行完成,返回码: %d" % exit_code)
|
||||||
|
|
||||||
|
response = 'Command %s processed as %s' % (buf, operation)
|
||||||
|
logging.debug("发送响应: '%s'" % response)
|
||||||
|
connection.send(response.encode('utf-8'))
|
||||||
|
logging.debug("响应发送完成")
|
||||||
|
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
||||||
logstr = "===" + datetime_str + " 客户端连接超时: " + address[0] + ":" + str(address[1]) + "==="
|
logstr = "===" + datetime_str + " 客户端连接超时: " + address[0] + ":" + str(address[1]) + "==="
|
||||||
@@ -104,8 +126,14 @@ def main():
|
|||||||
" 请求时发生错误: " + str(e) + "==="
|
" 请求时发生错误: " + str(e) + "==="
|
||||||
logging.error(logstr, exc_info=True)
|
logging.error(logstr, exc_info=True)
|
||||||
print("Error processing request:", e)
|
print("Error processing request:", e)
|
||||||
connection.send(('Error processing command: %s' % str(e)).encode('utf-8'))
|
try:
|
||||||
|
error_response = ('Error processing command: %s' % str(e)).encode('utf-8')
|
||||||
|
logging.debug("发送错误响应: '%s'" % error_response)
|
||||||
|
connection.send(error_response)
|
||||||
|
except Exception as send_err:
|
||||||
|
logging.error("发送错误响应失败: %s" % send_err)
|
||||||
finally:
|
finally:
|
||||||
|
logging.info("关闭客户端连接: %s:%s" % (address[0], address[1]))
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -33,8 +33,15 @@ COMMAND_MAPPINGS = {command_mappings}
|
|||||||
def process_command(buf):
|
def process_command(buf):
|
||||||
"""处理接收到的命令,返回对应的操作指令"""
|
"""处理接收到的命令,返回对应的操作指令"""
|
||||||
buf = buf.strip()
|
buf = buf.strip()
|
||||||
|
logging.debug("process_command: 原始输入='%s', 处理后='%s'" % (repr(buf), buf))
|
||||||
|
# 状态查询或心跳检测,返回特殊标记
|
||||||
|
if buf == "state=?" or buf == "":
|
||||||
|
logging.debug("process_command: 匹配到状态查询/心跳检测")
|
||||||
|
return "status_check"
|
||||||
if buf in COMMAND_MAPPINGS:
|
if buf in COMMAND_MAPPINGS:
|
||||||
|
logging.debug("process_command: 匹配到指令映射 '%s' -> '%s'" % (buf, COMMAND_MAPPINGS[buf]))
|
||||||
return COMMAND_MAPPINGS[buf]
|
return COMMAND_MAPPINGS[buf]
|
||||||
|
logging.debug("process_command: 未知指令 '%s'" % buf)
|
||||||
return "nodata"
|
return "nodata"
|
||||||
|
|
||||||
|
|
||||||
@@ -55,8 +62,11 @@ def main():
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
logging.debug("等待UDP数据包...")
|
||||||
# UDP 使用 recvfrom 接收数据,同时获取客户端地址
|
# UDP 使用 recvfrom 接收数据,同时获取客户端地址
|
||||||
buf_bytes, client_addr = sock.recvfrom(BUFSIZE)
|
buf_bytes, client_addr = sock.recvfrom(BUFSIZE)
|
||||||
|
logging.debug("收到原始数据: %s (长度=%d) 来自 %s:%d" %
|
||||||
|
(repr(buf_bytes), len(buf_bytes), client_addr[0], client_addr[1]))
|
||||||
buf = buf_bytes.decode('utf-8').strip()
|
buf = buf_bytes.decode('utf-8').strip()
|
||||||
|
|
||||||
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
||||||
@@ -66,13 +76,24 @@ def main():
|
|||||||
print(logstr)
|
print(logstr)
|
||||||
|
|
||||||
operation = process_command(buf)
|
operation = process_command(buf)
|
||||||
|
logging.debug("process_command 返回: '%s'" % operation)
|
||||||
|
|
||||||
if operation == "nodata":
|
if operation == "status_check":
|
||||||
|
# 状态查询或心跳检测,直接回复 ok
|
||||||
|
response = b'ok'
|
||||||
|
logstr = "===" + datetime_str + " 状态查询/心跳检测,回复: ok"
|
||||||
|
logging.info(logstr)
|
||||||
|
print(logstr)
|
||||||
|
logging.debug("发送UDP响应到 %s:%d: '%s'" % (client_addr[0], client_addr[1], response))
|
||||||
|
sock.sendto(response, client_addr)
|
||||||
|
logging.debug("UDP响应发送完成")
|
||||||
|
elif operation == "nodata":
|
||||||
|
response = ('Unknown command: %s' % buf).encode('utf-8')
|
||||||
logstr = "===" + datetime_str + " 未知指令: '" + str(buf) + "'"
|
logstr = "===" + datetime_str + " 未知指令: '" + str(buf) + "'"
|
||||||
logging.warning(logstr)
|
logging.warning(logstr)
|
||||||
print(logstr)
|
print(logstr)
|
||||||
# 发送UDP响应
|
logging.debug("发送UDP未知指令响应到 %s:%d: '%s'" % (client_addr[0], client_addr[1], response))
|
||||||
sock.sendto(('Unknown command: %s' % buf).encode('utf-8'), client_addr)
|
sock.sendto(response, client_addr)
|
||||||
else:
|
else:
|
||||||
logstr = "===" + datetime_str + " 映射到内部指令: '" + str(operation) + "'"
|
logstr = "===" + datetime_str + " 映射到内部指令: '" + str(operation) + "'"
|
||||||
logging.info(logstr)
|
logging.info(logstr)
|
||||||
@@ -83,10 +104,15 @@ def main():
|
|||||||
str(UPC_DEV_IP) + " " + str(UPC_DEV_PORT) + " " + \
|
str(UPC_DEV_IP) + " " + str(UPC_DEV_PORT) + " " + \
|
||||||
str(operation) + " " + str(LOG_FILE)
|
str(operation) + " " + str(LOG_FILE)
|
||||||
logging.info("执行外部命令: %s" % os_command)
|
logging.info("执行外部命令: %s" % os_command)
|
||||||
os.system(os_command)
|
logging.debug("开始执行外部命令...")
|
||||||
|
exit_code = os.system(os_command)
|
||||||
|
logging.debug("外部命令执行完成,返回码: %d" % exit_code)
|
||||||
|
|
||||||
|
response = ('Command %s processed as %s' % (buf, operation)).encode('utf-8')
|
||||||
|
logging.debug("发送UDP响应到 %s:%d: '%s'" % (client_addr[0], client_addr[1], response))
|
||||||
|
sock.sendto(response, client_addr)
|
||||||
|
logging.debug("UDP响应发送完成")
|
||||||
|
|
||||||
# 发送UDP响应
|
|
||||||
sock.sendto(('Command %s processed as %s' % (buf, operation)).encode('utf-8'), client_addr)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
||||||
logstr = "===" + datetime_str + " 处理UDP请求时发生错误: " + str(e) + "==="
|
logstr = "===" + datetime_str + " 处理UDP请求时发生错误: " + str(e) + "==="
|
||||||
|
|||||||
Reference in New Issue
Block a user