hel 1 năm trước cách đây
mục cha
commit
fa4b326ab7
4 tập tin đã thay đổi với 86 bổ sung29 xóa
  1. 23 19
      listenTest.py
  2. 29 0
      messageClass.py
  3. 19 4
      monitorTest.py
  4. 15 6
      queueFunc.py

+ 23 - 19
listenTest.py

@@ -5,13 +5,17 @@ from pynput.mouse import Controller, Button
 import asyncio
 
 import win32Test
+import pika
+import messageClass
+
+connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
+channel = connection.channel()
+
+channel.exchange_declare("sync", "fanout", durable=True)
 
 mouse_controller = Controller()
 
 middle_point, base_width, base_height = win32Test.arrange_windows()
-print(middle_point)
-print(base_width)
-print(base_height)
 
 
 def in_monitorZone():
@@ -23,7 +27,6 @@ def in_monitorZone():
 
 
 def on_click(x, y, button, pressed):
-
     # function:0
     # x:
     # y:
@@ -36,10 +39,8 @@ def on_click(x, y, button, pressed):
     # dx,
     # dy,
 
-    print('{0} at {1}'.format(
-        'Pressed' if pressed else 'Released',
-        (x, y)))
-    print(button)
+    message = messageClass.ClickMessage(x, y, button).getMessage()
+    channel.basic_publish(exchange='sync', routing_key='', body=message)
     # if not pressed:
     #     # Stop listener
     #     return False
@@ -54,17 +55,20 @@ async def scroll_to_next_point(middle_point, mouse_controller, dx, dy):
 
 
 def on_scroll(x, y, dx, dy):
-    print('Scrolled {0} at {1}'.format(
-        'down' if dy < 0 else 'up',
-        (x, y)))
-    if in_monitorZone():
-        origin = mouse_controller.position
-        for i in range(1, len(middle_point)):
-            print('scrolled')
-            mouse_controller.position = middle_point[i]
-            mouse_controller.click(Button.right, 1)
-            time.sleep(0.5)
-        mouse_controller.position = origin
+    message = messageClass.ScrollMessage(x, y, dx, dy).getMessage()
+    channel.basic_publish(exchange='sync', routing_key='', body=message)
+
+    # print('Scrolled {0} at {1}'.format(
+    #     'down' if dy < 0 else 'up',
+    #     (x, y)))
+    # if in_monitorZone():
+    #     origin = mouse_controller.position
+    #     for i in range(1, len(middle_point)):
+    #         print('scrolled')
+    #         mouse_controller.position = middle_point[i]
+    #         mouse_controller.click(Button.right, 1)
+    #         time.sleep(0.5)
+    #     mouse_controller.position = origin
 
 
 # Collect events until released

+ 29 - 0
messageClass.py

@@ -0,0 +1,29 @@
+import json
+
+from pynput.mouse import Controller, Button
+
+
+class ClickMessage:
+    def __init__(self, x, y, button):
+        self.function = 0
+        self.x = x
+        self.y = y
+        if button == Button.left:
+            self.button = "left"
+        elif button == Button.right:
+            self.button = "right"
+
+    def getMessage(self):
+        return json.dumps(self)
+
+
+class ScrollMessage:
+    def __init__(self, x, y, dx, dy):
+        self.function = 1
+        self.x = x
+        self.y = y
+        self.dx = dx
+        self.dy = dy
+
+    def getMessage(self):
+        return json.dumps(self)

+ 19 - 4
monitorTest.py

@@ -1,8 +1,8 @@
 #!/usr/bin/env python
 import os
 import pika
+import signal
 import sys
-
 import queueFunc
 
 connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.13', port=5672))
@@ -12,11 +12,10 @@ channel = connection.channel()
 def callback(ch, method, properties, body):
     # 处理消息的逻辑
     print(f"Received message: {body.decode('utf-8')}")
-
     # 执行其他操作,例如记录日志、更新数据库等
     queueFunc.handleMessage(properties, body)
     # 确认收到消息
-    ch.basic_ack(delivery_tag=method.delivery_tag)
+    # ch.basic_ack(delivery_tag=method.delivery_tag)
 
 
 def main():
@@ -26,7 +25,7 @@ def main():
 
     # 定义一个队列
     queue_name = 'hello'
-    channel.queue_declare(queue=queue_name)
+    channel.queue_declare(queue=queue_name, durable=True)
 
     # 绑定队列到交换机
     routing_key = ''
@@ -40,6 +39,16 @@ def main():
     channel.start_consuming()
 
 
+def signal_handler(sig, frame):
+    print("Caught signal, closing connection and channel")
+    channel.close()
+    connection.close()
+    sys.exit(0)
+
+
+signal.signal(signal.SIGINT, signal_handler)
+signal.signal(signal.SIGTERM, signal_handler)
+
 if __name__ == '__main__':
     try:
         main()
@@ -49,5 +58,11 @@ if __name__ == '__main__':
             channel.close()
             connection.close()
             sys.exit(0)
+
         except SystemExit:
+            channel.close()
+            connection.close()
             os._exit(0)
+    finally:
+        channel.close()
+        connection.close()

+ 15 - 6
queueFunc.py

@@ -1,20 +1,29 @@
 from pynput.mouse import Controller, Button
 import json
+import time
 
 mouseController = Controller()
 
 
 def handleMessage(properties, body):
     jsonObject = json.loads(body)
-    if jsonObject.function == 0:
-        on_click(jsonObject.x, jsonObject.y, jsonObject.button, jsonObject.pressed)
+    print(jsonObject)
+    print(time.time())
+    if jsonObject['function'] == 0:
+        on_click(jsonObject['x'], jsonObject['y'], jsonObject['button'])
     else:
-        on_scroll(jsonObject.x, jsonObject.y, jsonObject.dx, jsonObject.dy)
-    return
+        on_scroll(jsonObject['x'], jsonObject['y'], jsonObject['dx'], jsonObject['dy'])
 
 
-def on_click(x, y, button, pressed):
-    mouseController.click(x, y, button, 1)
+
+def on_click(x, y, button):
+    mouseController.position = (x, y)
+    if 'left' in button:
+        print('w')
+        mouseController.click(Button.left, 1)
+    else:
+        print('2')
+        mouseController.click(Button.right, 1)
 
 
 def on_scroll(x, y, dx, dy):