237 lines
8.8 KiB
Python
237 lines
8.8 KiB
Python
# import socket
|
||
# import threading
|
||
# import aacecontrol
|
||
# import ctypes,win32con,win32gui,sys,json
|
||
# import logging
|
||
# # import file_script
|
||
|
||
|
||
# IpAddress = '127.0.0.1'
|
||
# FilePath = 'D:\\MegaSoft\\Scripts'
|
||
# Port = 2016
|
||
# # device = md.MultiWinDevice("multiWin")
|
||
# device = aacecontrol.AppService()
|
||
# # 定义互斥量名称
|
||
# mutex_name = "aaca_mutex"
|
||
|
||
|
||
# def handle_client(client_socket,addr):
|
||
# device.loginformation(f'监听{addr}的数据')
|
||
# while True:
|
||
# try:
|
||
# # 接收客户端发送的数据
|
||
# data = client_socket.recv(2048)
|
||
# if not data:
|
||
# device.loginformation(f'{addr}-接收到空数据')
|
||
# break
|
||
# # # 处理接收到的数据
|
||
# # device.loginformation(f"Received from {client_socket.getpeername()}: {data.decode('utf-8')}")
|
||
# datadecode = data.decode('utf-8')
|
||
# command = datadecode.partition(':')[0] # 只分割一次
|
||
# paras = datadecode.partition(':')[2]
|
||
# result = -1
|
||
|
||
# if 'makeGraph' in command:
|
||
# command = 'makeGraph'
|
||
# device.loginformation(f'---收到制图指令 {datadecode}---')
|
||
# result = device.click_create_table(paras)
|
||
# elif 'operate' in command:
|
||
# command = 'operate'
|
||
# device.loginformation(f'---收到创建任务指令 {datadecode}---')
|
||
# result = device.start(paras)
|
||
# elif 'createTask' in command:
|
||
# command = 'createTask'
|
||
# device.loginformation(f'---收到制图创建任务命令 {datadecode}---')
|
||
# result = device.create(paras)
|
||
# elif 'start' in command:
|
||
# command = 'start'
|
||
# device.loginformation(f'---收到启动命令 {datadecode}---')
|
||
# result = device.run(paras)
|
||
# elif 'status' in command:
|
||
# device.loginformation(f'---收到查询状态命令 {datadecode}---')
|
||
# result = 2
|
||
# if device.is_finished():
|
||
# result = 1
|
||
# else:
|
||
# command = '未知命令'
|
||
# device.loginformation('---未知命令---')
|
||
|
||
# reponse_txt = command + ':' + str(result) + '\n'
|
||
# client_socket.send(reponse_txt.encode('utf-8'))
|
||
# device.loginformation(f'---返回-{reponse_txt}---')
|
||
# except:
|
||
# device.loginformation(f'{addr}-接收数据异常')
|
||
# client_socket.close()
|
||
# return
|
||
# # 关闭连接
|
||
# device.loginformation(f'{addr}-连接断开')
|
||
# client_socket.close()
|
||
|
||
# def start_server():
|
||
# server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
# server.bind((IpAddress, Port))
|
||
# server.listen(20)
|
||
# device.loginformation(f"Server listening on port {IpAddress}:{Port}...")
|
||
# try:
|
||
# while True:
|
||
# client_socket, addr = server.accept()
|
||
# device.loginformation(f"Accepted connection from {addr}")
|
||
# # 启动一个新线程来处理客户端请求
|
||
# client_handler = threading.Thread(target=handle_client, args=(client_socket,addr))
|
||
# client_handler.start()
|
||
# except:
|
||
# print('服务结束了。。。')
|
||
# finally:
|
||
# ctypes.windll.kernel32.CloseHandle(mutex)
|
||
|
||
# def message_box(title, message, type=win32con.MB_OK):
|
||
# """
|
||
# 弹出一个消息框
|
||
# :param title: 消息框的标题
|
||
# :param message: 消息框显示的消息
|
||
# :param type: 消息框的类型,例如MB_OK, MB_ICONERROR等
|
||
# :return: 用户点击的消息框按钮的标识符
|
||
# """
|
||
# return win32gui.MessageBox(None, message, title, type)
|
||
|
||
# if __name__ == "__main__":
|
||
# # 定义互斥锁名称,可以是任意字符串
|
||
# mutex = ctypes.windll.kernel32.CreateMutexW(None, False, mutex_name)
|
||
# # 检查互斥量是否已经存在
|
||
# if ctypes.windll.kernel32.GetLastError() == 183: # ERROR_ALREADY_EXISTS
|
||
# message_box("提醒", "脚本正在运行中,请点击确定关闭。", win32con.MB_ICONWARNING)
|
||
# sys.exit(0)
|
||
|
||
# ctypes.windll.kernel32.SetThreadExecutionState(0x80000002) # 确保桌面不息屏
|
||
|
||
# server_thread = threading.Thread(target=start_server)
|
||
# server_thread.start()
|
||
|
||
# # w = file_script.Watcher(FilePath)
|
||
# # file_thread = threading.Thread(target=w.run)
|
||
# # file_thread.start()
|
||
|
||
|
||
import ctypes
|
||
import socket
|
||
import sys
|
||
import threading
|
||
import win32con
|
||
import win32gui
|
||
from file_script import Watcher
|
||
import aacecontrol
|
||
|
||
|
||
IpAddress = '127.0.0.1'
|
||
FilePath = 'D:\\MegaSoft\\Scripts'
|
||
Port = 2016
|
||
device = aacecontrol.AppService()
|
||
# 定义互斥量名称
|
||
mutex_name = "aaca_mutex"
|
||
|
||
def handle_client(client, addr):
|
||
while True:
|
||
try:
|
||
data = client.recv(2048)
|
||
if not data:
|
||
device.loginformation("接收数据异常")
|
||
break
|
||
datadecode = data.decode('utf-8')
|
||
device.loginformation(f"接收到---{client.getpeername()}---数据---{datadecode}")
|
||
result = -1
|
||
command = datadecode.partition(':')[0] # 只分割一次
|
||
paras = datadecode.partition(':')[2] # 返回包含分隔符
|
||
|
||
# if 'makeGraph' in command:
|
||
# command = 'makeGraph'
|
||
# device.loginformation(f'---收到制图指令 {datadecode}---')
|
||
# result = device.click_create_table(paras)
|
||
# elif 'operate' in command:
|
||
# command = 'operate'
|
||
# device.loginformation(f'---收到创建任务指令 {datadecode}---')
|
||
# result = device.start(paras)
|
||
if 'createTask' in command:
|
||
command = 'createTask'
|
||
device.loginformation(f'收到创建任务并执行命令 {datadecode}')
|
||
result = device.createThenStart(paras)
|
||
# elif 'start' in command:
|
||
# command = 'start'
|
||
# device.loginformation(f'---收到启动命令 {datadecode}---')
|
||
# result = device.run(paras)
|
||
elif 'status' in command:
|
||
device.loginformation(f'收到查询状态命令 {datadecode}')
|
||
result = 2
|
||
if device.is_finished():
|
||
result = 0
|
||
else:
|
||
command = '未知命令'
|
||
device.loginformation('未知命令')
|
||
|
||
response_txt = command + ':' + str(result) + '\n'
|
||
device.loginformation(f'返回:{command}:{str(result)}')
|
||
client.send(response_txt.encode('utf-8'))
|
||
except:
|
||
device.loginformation(f'{addr}通讯异常')
|
||
break
|
||
# 关闭连接
|
||
device.loginformation(f'关闭{addr}连接')
|
||
client.close()
|
||
|
||
def message_box(title, message, type=win32con.MB_OK):
|
||
"""
|
||
弹出一个消息框
|
||
:param title: 消息框的标题
|
||
:param message: 消息框显示的消息
|
||
:param type: 消息框的类型,例如MB_OK, MB_ICONERROR等
|
||
:return: 用户点击的消息框按钮的标识符
|
||
"""
|
||
return win32gui.MessageBox(None, message, title, type)
|
||
|
||
|
||
class TCPThread(threading.Thread):
|
||
def run(self):
|
||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
server.bind((IpAddress, Port))
|
||
server.listen(20)
|
||
device.loginformation(f"服务端---{IpAddress}---{Port}---启动")
|
||
connected_clients = 0
|
||
try:
|
||
while True:
|
||
client, addr = server.accept()
|
||
connected_clients = connected_clients + 1
|
||
device.loginformation(f"客户端---{addr}---已连接,已连接客户端数量:{connected_clients}")
|
||
client_handle = threading.Thread(target=handle_client, args=(client,addr))
|
||
client_handle.start()
|
||
except Exception as e:
|
||
device.loginformation(f"{str(e)}服务端正在关闭...")
|
||
server.close()
|
||
device.loginformation("服务端已关闭")
|
||
finally:
|
||
# 释放互斥量
|
||
ctypes.windll.kernel32.CloseHandle(mutex)
|
||
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 定义互斥锁名称,可以是任意字符串
|
||
mutex = ctypes.windll.kernel32.CreateMutexW(None, False, mutex_name)
|
||
# 检查互斥量是否已经存在
|
||
if ctypes.windll.kernel32.GetLastError() == 183: # ERROR_ALREADY_EXISTS
|
||
message_box("提醒", "脚本正在运行中,请点击确定关闭。", win32con.MB_ICONWARNING)
|
||
sys.exit(0)
|
||
|
||
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
|
||
|
||
tcp_thread = TCPThread()
|
||
tcp_thread.start()
|
||
|
||
# 创建线程
|
||
watcher = Watcher(FilePath)
|
||
watcher_thread = threading.Thread(target=watcher.run)
|
||
watcher_thread.start()
|
||
|
||
|
||
|
||
|
||
|