Files
UPC-Resent/bin/dev2.py
2026-03-22 17:17:44 +08:00

125 lines
4.3 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Auto-generated from template. DO NOT EDIT DIRECTLY!
# Device: 设备125-TCP (dev2)
import socket
import os
import time
import logging
import sys
# ====== 配置参数(从配置文件读取)======
TMS_SERVER_IP = '192.168.8.9'
TMS_PORT = 10129
UPC_DEV_IP = '192.168.8.125'
UPC_DEV_PORT = '502'
SENDER_FILE = '/home/smart/pythonPJ/upc_resent/bin/sender_tcp.py'
LOG_FILE = '/home/smart/pythonPJ/upc_resent/log/dev2.log'
PYTHON_PATH = '/usr/bin/python3'
DEVICE_ID = 'dev2'
# =========================
BUFSIZE = 1024
# 配置日志
logging.basicConfig(filename=LOG_FILE, filemode="a", level=logging.DEBUG)
ISOTIMEFORMAT = '%Y-%m-%d %X'
# 命令映射表
COMMAND_MAPPINGS = {
"open": "openall4",
"close": "closeall4",
"guanggao-guan": "closeall4",
"open1": "open1",
"close1": "close1",
"open2": "open2",
"close2": "close2",
"open3": "open3",
"close3": "close3",
"open4": "open4",
"close4": "close4",
"open5": "open5",
"close5": "close5",
"open6": "open6",
"close6": "close6",
"open7": "open7",
"close7": "close7",
"open8": "open8",
"close8": "close8",
}
def process_command(buf):
"""处理接收到的命令,返回对应的操作指令"""
buf = buf.strip()
if buf in COMMAND_MAPPINGS:
return COMMAND_MAPPINGS[buf]
return "nodata"
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind((TMS_SERVER_IP, int(TMS_PORT)))
sock.listen(500)
except OSError as e:
logstr = "!!!!!ERROR!!!!!" + time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) + \
" 无法绑定端口 %s:%s - %s" % (TMS_SERVER_IP, TMS_PORT, e)
logging.error(logstr)
print(logstr)
sys.exit(1)
logging.info("服务启动,监听 %s:%s" % (TMS_SERVER_IP, TMS_PORT))
print(f"[{DEVICE_ID}] 服务启动,监听 {TMS_SERVER_IP}:{TMS_PORT}")
while True:
connection, address = sock.accept()
try:
connection.settimeout(5)
buf_bytes = connection.recv(BUFSIZE)
buf = buf_bytes.decode('utf-8').strip()
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
logstr = "===" + datetime_str + " 收到客户端 " + address[0] + ":" + str(address[1]) + \
" 发送的指令: '" + str(buf) + "'"
logging.info(logstr)
print(logstr)
operation = process_command(buf)
if operation == "nodata":
logstr = "===" + datetime_str + " 未知指令: '" + str(buf) + "'"
logging.warning(logstr)
print(logstr)
connection.send(('Unknown command: %s' % buf).encode('utf-8'))
else:
logstr = "===" + datetime_str + " 映射到内部指令: '" + str(operation) + "'"
logging.info(logstr)
print(logstr)
os_command = str(PYTHON_PATH) + " " + SENDER_FILE + " " + \
str(UPC_DEV_IP) + " " + str(UPC_DEV_PORT) + " " + \
str(operation) + " " + str(LOG_FILE)
logging.info("执行外部命令: %s" % os_command)
os.system(os_command)
connection.send(('Command %s processed as %s' % (buf, operation)).encode('utf-8'))
except socket.timeout:
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
logstr = "===" + datetime_str + " 客户端连接超时: " + address[0] + ":" + str(address[1]) + "==="
logging.warning(logstr)
print('time out')
except Exception as e:
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
logstr = "===" + datetime_str + " 处理客户端 " + address[0] + ":" + str(address[1]) + \
" 请求时发生错误: " + str(e) + "==="
logging.error(logstr, exc_info=True)
print("Error processing request:", e)
connection.send(('Error processing command: %s' % str(e)).encode('utf-8'))
finally:
connection.close()
if __name__ == '__main__':
main()