119 lines
4.0 KiB
Python
Executable File
119 lines
4.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
# -*- coding: UTF-8 -*-
|
||
# Auto-generated from template. DO NOT EDIT DIRECTLY!
|
||
# Device: 设备UDP示例 (dev3) - UDP Protocol
|
||
|
||
import socket
|
||
import os
|
||
import time
|
||
import logging
|
||
import sys
|
||
|
||
# ====== 配置参数(从配置文件读取)======
|
||
TMS_SERVER_IP = '192.168.8.9'
|
||
TMS_PORT = 10080
|
||
UPC_DEV_IP = '192.168.8.200'
|
||
UPC_DEV_PORT = 502
|
||
SENDER_FILE = '/home/smart/pythonPJ/upc_resent/bin/sender_udp.py'
|
||
LOG_FILE = '/home/smart/pythonPJ/upc_resent/log/dev3.log'
|
||
PYTHON_PATH = '/usr/bin/python3'
|
||
DEVICE_ID = 'dev3'
|
||
# =========================
|
||
|
||
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():
|
||
# 创建 UDP socket
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||
try:
|
||
sock.bind((TMS_SERVER_IP, int(TMS_PORT)))
|
||
except OSError as e:
|
||
logstr = "!!!!!ERROR!!!!!" + time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) + \
|
||
" 无法绑定UDP端口 %s:%s - %s" % (TMS_SERVER_IP, TMS_PORT, e)
|
||
logging.error(logstr)
|
||
print(logstr)
|
||
sys.exit(1)
|
||
|
||
logging.info("UDP服务启动,监听 %s:%s" % (TMS_SERVER_IP, TMS_PORT))
|
||
print(f"[{DEVICE_ID}] UDP服务启动,监听 {TMS_SERVER_IP}:{TMS_PORT}")
|
||
|
||
while True:
|
||
try:
|
||
# UDP 使用 recvfrom 接收数据,同时获取客户端地址
|
||
buf_bytes, client_addr = sock.recvfrom(BUFSIZE)
|
||
buf = buf_bytes.decode('utf-8').strip()
|
||
|
||
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
||
logstr = "===" + datetime_str + " 收到UDP客户端 " + client_addr[0] + ":" + str(client_addr[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)
|
||
# 发送UDP响应
|
||
sock.sendto(('Unknown command: %s' % buf).encode('utf-8'), client_addr)
|
||
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)
|
||
|
||
# 发送UDP响应
|
||
sock.sendto(('Command %s processed as %s' % (buf, operation)).encode('utf-8'), client_addr)
|
||
except Exception as e:
|
||
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
|
||
logstr = "===" + datetime_str + " 处理UDP请求时发生错误: " + str(e) + "==="
|
||
logging.error(logstr, exc_info=True)
|
||
print("Error processing UDP request:", e)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|