This repository has been archived by the owner on May 7, 2023. It is now read-only.
forked from krabo0om/docker-image-adb-mqtt-proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadb_monitor.py
executable file
·249 lines (216 loc) · 8.27 KB
/
adb_monitor.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
import logging
import os
import signal
import socket
import sys
import time
import ssl
import paho.mqtt.client as mqtt
import socket
from subprocess import check_output
# Script name (without extension) used for config/logfile names
HOSTNAME = socket.gethostname()
APPNAME = os.path.splitext(os.path.basename(__file__))[0]
MQTT_HOST = os.environ['MQTT_SERVER']
MQTT_PORT = int(os.environ['MQTT_PORT'])
MQTT_USERNAME = os.environ['USER']
MQTT_PASSWORD = os.environ['PASSWORD']
MQTT_CLIENT_ID = os.environ['MQTT_CLIENT'] if 'MQTT_CLIENT' in os.environ else HOSTNAME
MQTT_TOPIC = os.environ['TOPIC']
ADB_DEVICES = os.environ['ADB_DEVICE'].split(",")
POLL_INTERVAL = int(os.environ['POLL_INTERVAL'])
DEBUG = True if 'DEBUG' in os.environ else False
# other MQTT settings
MQTT_QOS = 2
MQTT_RETAIN = True
MQTT_CLEAN_SESSION = True
MQTT_LWT = "clients/%s" % MQTT_CLIENT_ID
# Initialise logger
LOGFORMAT = '%(asctime)-15s %(levelname)-5s %(message)s'
logger = logging.getLogger(APPNAME)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO) # loglevel for console
if DEBUG:
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.info("Starting " + APPNAME)
for adb_device in ADB_DEVICES:
logger.info("Monitoring ADB Device: %s" % adb_device)
logger.info(" Publish ADB Stats to topic: %s/%s/stat" % (MQTT_TOPIC, adb_device))
logger.info(" Monitor ADB commands on topic: %s/%s/cmd" % (MQTT_TOPIC, adb_device))
# Create the MQTT client
mqttc = mqtt.Client(MQTT_CLIENT_ID, clean_session=MQTT_CLEAN_SESSION)
# keep states in disconnect
STATES = {}
# MQTT callbacks
def on_connect(mosq, obj, flags, result_code):
"""
Handle connections (or failures) to the broker.
This is called after the client has received a CONNACK message
from the broker in response to calling connect().
The parameter rc is an integer giving the return code:
0: Success
1: Refused . unacceptable protocol version
2: Refused . identifier rejected
3: Refused . server unavailable
4: Refused . bad user name or password (MQTT v3.1 broker only)
5: Refused . not authorised (MQTT v3.1 broker only)
"""
if result_code == 0:
logger.info("Connected to %s:%s" % (MQTT_HOST, MQTT_PORT))
# Subscribe incoming topic
for adb_device in ADB_DEVICES:
mqtt_topic = "%s/%s/cmd" %(MQTT_TOPIC, adb_device)
logger.debug("subscribing to topic %s" % mqtt_topic)
mqttc.subscribe(mqtt_topic, qos=MQTT_QOS)
# Publish retained LWT as per http://stackoverflow.com/questions/19057835/how-to-find-connected-mqtt-client-details/19071979#19071979
# See also the will_set function in connect() below
mqttc.publish(MQTT_LWT, "1", qos=0, retain=True)
elif result_code == 1:
logger.info("Connection refused - unacceptable protocol version")
elif result_code == 2:
logger.info("Connection refused - identifier rejected")
elif result_code == 3:
logger.info("Connection refused - server unavailable")
elif result_code == 4:
logger.info("Connection refused - bad user name or password")
elif result_code == 5:
logger.info("Connection refused - not authorised")
else:
logger.warning("Connection failed - result code %d" % (result_code))
def on_disconnect(mosq, obj, result_code):
"""
Handle disconnections from the broker
"""
if result_code == 0:
logger.info("Clean disconnection from broker")
else:
logger.warning("Broker connection lost. Retrying in 5s...")
time.sleep(5)
def on_message(mosq, obj, msg):
"""
Handle incoming messages
"""
logger.debug("Received MQTT message --> topic: %s - payload: %s" % (msg.topic, msg.payload))
topicparts = msg.topic.split("/")
adb_device = topicparts[-2]
value = msg.payload.strip()
logger.debug("Incoming message for device %s -> %s" % (adb_device, value))
if adb_device not in ADB_DEVICES:
"Requested device is not monitored !"
elif value in ["1", "on", "ON"]:
adb_command(adb_device, "shell input keyevent KEYCODE_WAKEUP")
elif value in ["0", "off", "OFF"]:
adb_command(adb_device, "shell input keyevent KEYCODE_SLEEP")
else:
# custom adb command
adb_command(adb_device, value)
publish_state()
# End of MQTT callbacks
def adb_command(adb_device, adb_cmd):
'''issue adb command'''
output = ""
if adb_cmd and adb_cmd.startswith("shell") and adb_cmd != "shell":
# issue adb command
adb_cmd = "adb -s %s:5555 %s" %(adb_device, adb_cmd)
try:
output = check_output(adb_cmd, shell=True, universal_newlines=True)
logger.debug(output)
except Exception as exc:
logger.debug(exc)
if not output or "error" in output:
logger.debug("ADB command failed - issue reconnect...")
adb_connect()
try:
output = check_output(adb_cmd, shell=True, universal_newlines=True)
logger.debug(output)
except Exception as exc:
logger.debug(exc)
else:
logger.error("Invalid command, only shell commands are supported")
return output
def adb_connect():
'''conect adb devices'''
logger.debug("Connect to ADB service ")
for adb_device in ADB_DEVICES:
adb_device = adb_device + ":5555"
for cmd in ["disconnect", "connect"]:
adb_cmd = 'adb %s %s' % (cmd, adb_device)
try:
output = check_output(adb_cmd, shell=True, universal_newlines=True)
logger.debug(output)
except Exception as exc:
logger.debug(exc)
def publish_state():
''' publish state of adb devices'''
for adb_device in ADB_DEVICES:
state = adb_command(adb_device, "shell dumpsys power | grep Display\ Power:\ state=")
if "Display Power: state=ON" in state:
state = "ON"
else:
state = "OFF"
if STATES.get(adb_device,"") != state:
logger.debug("State changed for device %s - new state: %s" %(adb_device, state))
mqtt_topic = "%s/%s/stat" % (MQTT_TOPIC, adb_device)
mqttc.publish(mqtt_topic, payload=state, qos=MQTT_QOS, retain=MQTT_RETAIN)
STATES[adb_device] = state
def cleanup(signum, frame):
"""
Signal handler to ensure we disconnect cleanly
in the event of a SIGTERM or SIGINT.
"""
# Publish our LWT and cleanup the MQTT connection
logger.info("Disconnecting from broker...")
mqttc.publish(MQTT_LWT, "0", qos=0, retain=True)
mqttc.disconnect()
mqttc.loop_stop()
# Exit from our application
logger.info("Exiting on signal %d" % (signum))
sys.exit(signum)
def connect():
"""
Connect to the broker, define the callbacks, and subscribe
This will also set the Last Will and Testament (LWT)
The LWT will be published in the event of an unclean or
unexpected disconnection.
"""
# Add the callbacks
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_message = on_message
# Set the login details
if MQTT_USERNAME:
mqttc.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
# Set the Last Will and Testament (LWT) *before* connecting
mqttc.will_set(MQTT_LWT, payload="0", qos=0, retain=True)
# Attempt to connect
logger.debug("Connecting to %s:%d..." % (MQTT_HOST, MQTT_PORT))
try:
mqttc.connect(MQTT_HOST, MQTT_PORT, 60)
except Exception as e:
logger.error("Error connecting to %s:%d: %s" % (MQTT_HOST, MQTT_PORT, str(e)))
sys.exit(2)
# Let the connection run forever
mqttc.loop_start()
def poll():
"""
The main loop in which we monitor the state of the devices
and publish any changes.
"""
while True:
try:
# monitor states
publish_state()
except Exception as exc:
logger.error(str(exc))
time.sleep(POLL_INTERVAL)
# Use the signal module to handle signals
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sig, cleanup)
# Connect to broker and begin polling our GPIO pins
connect()
poll()