-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus-only.py
108 lines (77 loc) · 2.65 KB
/
status-only.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python -u
import paho.mqtt.client as mqttClient
import paho.mqtt.publish as publish
import time
import serial
import json
import threading
import io
import argparse
import logging
import logging.handlers as handlers
lock = threading.Lock()
status_command = '* 0 Lamp ?'
logger = logging.getLogger("Acer-MQTT-Log")
previous_status = 'OFF'
def setup_logger():
logger.setLevel(logging.INFO)
logHandler = handlers.TimedRotatingFileHandler('acer-mqtt.log', when='D', interval=1, backupCount=1)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
def get_projector():
return serial.Serial(
port=globals()['serial_port'],
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2,
writeTimeout=0
)
def get_io_wrapper(projector):
return io.TextIOWrapper(
io.BufferedRWPair(projector, projector, 1),
newline='\r',
line_buffering=True
)
def get_status():
try:
lock.acquire()
projector = get_projector()
print(projector)
ser_io = get_io_wrapper(projector)
print(projector.isOpen())
if projector.isOpen():
projector.flushInput()
projector.flushOutput()
ser_io.write(unicode(status_command + '\r'))
print(projector.inWaiting())
for x in range(0, 5):
time.sleep(1)
if projector.inWaiting() > 0:
break
print(projector.inWaiting())
while projector.inWaiting() > 0:
line = ser_io.readline();
if not line.startswith('*'):
status = str(line.strip())
if status.endswith('0'):
return 'OFF'
else:
return 'ON'
else:
logger.error('Projector connection closed')
finally:
#print('closing projector')
projector.close()
#print('releasing the lock')
lock.release()
parser = argparse.ArgumentParser()
parser.add_argument('-sp', '--serialport', help='Serial port for acer projector (Required)', required=True)
args = vars(parser.parse_args())
setup_logger()
globals()['serial_port'] = args['serialport']
status = get_status()
logger.info("Current status %s" % status)
previous_status = status