projector_control.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import threading
  2. import time
  3. import socket
  4. class ProjectorControl(threading.Thread):
  5. def __init__(self, ip, port, type=1):
  6. threading.Thread.__init__(self)
  7. self.lock = threading.Lock()
  8. self.sock = None
  9. self.ip = ip
  10. self.port = port
  11. self.statu = None
  12. self.close = False
  13. if type == 1:
  14. self.open_str = "41 54 2B 4C 69 67 68 74 53 6F 75 72 63 65 3D 4F 6E 0D"
  15. self.close_str = "41 54 2B 4C 69 67 68 74 53 6F 75 72 63 65 3D 4F 66 66 0D"
  16. else:
  17. self.open_str = "25 31 41 56 4d 54 20 33 30 0d"
  18. self.close_str = "25 31 41 56 4d 54 20 33 31 0d"
  19. def lightOn(self):
  20. self.lock.acquire()
  21. self.statu = "open"
  22. self.lock.release()
  23. def lightOff(self):
  24. self.lock.acquire()
  25. self.statu = "close"
  26. self.lock.release()
  27. def exit(self):
  28. self.close = True
  29. def run(self):
  30. while self.close is not True:
  31. if self.statu is not None and self.statu == "open":
  32. self.open_illuminant()
  33. self.lock.acquire()
  34. self.statu = None
  35. self.lock.release()
  36. elif self.statu is not None and self.statu == "close":
  37. self.close_illuminant()
  38. self.lock.acquire()
  39. self.statu = None
  40. self.lock.release()
  41. time.sleep(0.01)
  42. def open_illuminant(self):
  43. t_time = time.time()
  44. while (time.time() - t_time) < 3:
  45. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  46. self.sock.settimeout(1)
  47. try:
  48. self.sock.connect((self.ip, self.port))
  49. self.sock.send(bytes.fromhex(self.open_str))
  50. print(bytes.fromhex(self.open_str))
  51. print("open projector \033[0:32mSUCCEND\033[m ")
  52. break
  53. except Exception as e:
  54. print("open projector \033[0:31mERROR\033[m: %s" % e.args)
  55. time.sleep(0.01)
  56. time.sleep(0.5)
  57. self.sock.close()
  58. def close_illuminant(self):
  59. t_time = time.time()
  60. while (time.time() - t_time) < 3:
  61. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  62. self.sock.settimeout(1)
  63. try:
  64. self.sock.connect((self.ip, self.port))
  65. self.sock.send(bytes.fromhex(self.close_str))
  66. print(bytes.fromhex(self.close_str))
  67. print("close projector \033[0:32mSUCCEND\033[m ")
  68. break
  69. except Exception as e:
  70. print("close projector \033[0:31mERROR\033[m: %s" % e.args)
  71. time.sleep(0.01)
  72. time.sleep(0.5)
  73. self.sock.close()
  74. # 按间距中的绿色按钮以运行脚本。
  75. # if __name__ == '__main__':
  76. # p = ProjectorControl("192.168.1.191", 4352, 2)
  77. # p.start()
  78. # p.open()