monitorTest.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. import signal
  5. import sys
  6. import time
  7. import pika
  8. from pynput.mouse import Controller, Button
  9. import screen
  10. screenNumber = screen.arrange_slave()
  11. connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.13', port=5672))
  12. channel = connection.channel()
  13. mouseController = Controller()
  14. def callback(ch, method, properties, body):
  15. # 处理消息的逻辑
  16. print(f"Received message: {body.decode('utf-8')}")
  17. print(time.time())
  18. # 执行其他操作,例如记录日志、更新数据库等
  19. handleMessage(properties, body)
  20. # 确认收到消息
  21. # ch.basic_ack(delivery_tag=method.delivery_tag)
  22. def main():
  23. # 定义一个交换机(exchange)
  24. exchange_name = 'sync'
  25. channel.exchange_declare(exchange=exchange_name, exchange_type='fanout', durable=True)
  26. # 定义一个队列
  27. queue_name = 'hello'
  28. channel.queue_declare(queue=queue_name, auto_delete=True)
  29. # 绑定队列到交换机
  30. routing_key = ''
  31. channel.queue_bind(exchange=exchange_name, queue=queue_name, routing_key=routing_key)
  32. print(f'Queue {queue_name} is now bound to exchange {exchange_name} with routing key {routing_key}')
  33. channel.basic_qos(prefetch_count=1)
  34. channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
  35. channel.start_consuming()
  36. def signal_handler(sig, frame):
  37. print("Caught signal, closing connection and channel")
  38. channel.close()
  39. connection.close()
  40. sys.exit(0)
  41. def handleMessage(properties, body):
  42. print('handle')
  43. jsonObject = json.loads(body)
  44. if jsonObject['function'] == 0:
  45. on_click(jsonObject['x'], jsonObject['y'], jsonObject['button'])
  46. else:
  47. on_scroll(jsonObject['x'], jsonObject['y'], jsonObject['dx'], jsonObject['dy'])
  48. def on_click(x, y, button):
  49. counter = 0
  50. for i in range(0, screenNumber):
  51. mouseController.position = (x + i * 500, y)
  52. # time.sleep(0.05)
  53. if 'left' in button:
  54. mouseController.press(Button.left)
  55. time.sleep(0.1)
  56. mouseController.release(Button.left)
  57. # mouseController.click(Button.left, 1)
  58. counter = counter + 1
  59. print(counter)
  60. else:
  61. mouseController.click(Button.right, 1)
  62. counter = counter + 1
  63. print(counter)
  64. def on_scroll(x, y, dx, dy):
  65. counter = 0
  66. for i in range(0, screenNumber):
  67. mouseController.position = (x + i * 500, y)
  68. mouseController.scroll(dx, dy)
  69. print(counter)
  70. signal.signal(signal.SIGINT, signal_handler)
  71. signal.signal(signal.SIGTERM, signal_handler)
  72. if __name__ == '__main__':
  73. try:
  74. main()
  75. except KeyboardInterrupt:
  76. print('Interrupted')
  77. try:
  78. channel.close()
  79. connection.close()
  80. sys.exit(0)
  81. except SystemExit:
  82. channel.close()
  83. connection.close()
  84. os._exit(0)
  85. finally:
  86. channel.close()
  87. connection.close()