-
Notifications
You must be signed in to change notification settings - Fork 22
/
mtda-ui
executable file
·431 lines (347 loc) · 12.6 KB
/
mtda-ui
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
# ---------------------------------------------------------------------------
# Command Line Interface for MTDA
# ---------------------------------------------------------------------------
#
# This software is a part of MTDA.
# Copyright (C) 2024 Siemens Digital Industries Software
#
# ---------------------------------------------------------------------------
# SPDX-License-Identifier: MIT
# ---------------------------------------------------------------------------
import os
import socket
import sys
import threading
import time
from py3qterm import TerminalWidget
from py3qterm.backend import Session
from py3qterm.backend import Terminal
from PyQt5.Qt import Qt
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import (QMainWindow, QAction, QApplication,
QHBoxLayout, QListWidget, QListWidgetItem,
QSizePolicy, QStyle, QTabWidget, QWidget)
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from mtda.client import Client
from mtda.console.screen import ScreenOutput
import mtda.constants as CONSTS
import mtda.discovery
class DeviceConsole(TerminalWidget):
def __init__(self, parent):
super().__init__(parent)
def agent(self):
if Session._mux is not None:
return Session._mux.agent(self._session._session_id)
return None
def execute(self, remote=None):
if remote is None:
remote = os.getenv("MTDA_REMOTE", None)
if self._session is not None:
self._session.stop()
self._session = DeviceSession()
if remote is not None:
self._session.start(remote)
self._timer_id = None
self.focusInEvent(None)
def mouseDoubleClickEvent(self, event):
pass
def mouseMoveEvent(self, event):
pass
def mousePressEvent(self, event):
self.focusInEvent(None)
def mouseReleaseEvent(self, event):
pass
def screen(self):
if Session._mux is not None:
return Session._mux.screen(self._session._session_id)
return None
class DeviceMux:
def __init__(self):
self.session = {}
def agent(self, sid):
if sid in self.session:
return self.session[sid]['agent']
return None
def proc_bury(self, sid):
if sid not in self.session:
return False
agent = self.agent(sid)
# Detach remote consoles
agent.console_remote(None, None)
agent.monitor_remote(None, None)
# Disconnect from agent
agent.stop()
# Session was buried
return True
def proc_keepalive(self, sid, w, h, cmd=None):
if sid not in self.session:
# Start a new session
self.remote = cmd
agent = Client(self.remote)
term = Terminal(w, h)
self.session[sid] = {
'agent': agent,
'state': 'unborn',
'term': term,
'time': time.time(),
'w': w,
'h': h
}
screen = DeviceScreen(agent, term, self.session[sid])
agent.console_remote(self.remote, screen)
agent.monitor_remote(self.remote, screen)
agent.start()
self.session[sid]['state'] = 'alive'
self.session[sid]['screen'] = screen
os.environ["COLUMNS"] = str(w)
os.environ["ROWS"] = str(h)
return True
def proc_dump(self, sid):
if sid not in self.session:
return False
return self.session[sid]['term'].dump()
def proc_write(self, sid, d):
if sid not in self.session:
return False
elif self.session[sid]['state'] != 'alive':
return False
term = self.session[sid]['term']
d = term.pipe(d)
agent = self.session[sid]['agent']
agent.console_send(d)
return True
def screen(self, sid):
if sid in self.session:
return self.session[sid]['screen']
return None
def stop(self):
return None
class DeviceScreen(ScreenOutput):
def __init__(self, mtda, term, session):
super().__init__(mtda)
self._event_handler = None
self._term = term
self._session = session
def add_listener(self, handler):
self._event_handler = handler
def on_event(self, event):
if self._event_handler is not None:
event = event.split()
self._event_handler(event[0], event[1:])
def write(self, data):
self._term.write(data)
data = self._term.read()
self._session["changed"] = time.time()
class DeviceSession(Session):
def __init__(self):
super().__init__()
Session._mux = DeviceMux()
class MultiTenantDeviceAccessWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.service_watcher = mtda.discovery.Watcher(
CONSTS.MDNS.TYPE,
self.service_event)
self.service_thread = threading.Thread(
target=self.service_watcher.listen,
daemon=True,
name='service discovery')
self.service_thread.start()
def capture(self):
screen = self.console.screen()
if screen is not None:
if screen.capture_enabled() is True:
self.capture_stop()
else:
self.capture_start()
def capture_reset(self):
agent = self.console.agent()
enabled = (agent is not None)
self.captureAct.setEnabled(enabled)
def capture_start(self):
screen = self.console.screen()
if screen is not None:
if screen.capture_enabled() is False:
screen.capture_start()
icn = self.style().standardIcon(QStyle.SP_BrowserStop)
self.captureAct.setIcon(icn)
def capture_stop(self):
screen = self.console.screen()
if screen is not None:
if screen.capture_enabled() is True:
screen.capture_stop()
icn = self.style().standardIcon(QStyle.SP_DialogSaveButton)
self.captureAct.setIcon(icn)
def device_event(self, event, data):
if event == "POWER":
self.power_event(data[0])
elif event == "STORAGE":
self.storage_event(data[0])
def exit(self):
self.service_watcher.shutdown()
self.close()
def initUI(self):
self.root_widget = QWidget()
self.hlayout = QHBoxLayout()
self.root_widget.setLayout(self.hlayout)
self.setCentralWidget(self.root_widget)
self.services = QListWidget()
self.services.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
self.services.itemDoubleClicked.connect(self.service_change)
self.hlayout.addWidget(self.services)
self.tabs = QTabWidget(self.root_widget)
self.tabs.setTabBarAutoHide(True)
self.hlayout.addWidget(self.tabs)
self.console = DeviceConsole(self.tabs)
self.tabs.addTab(self.console, 'Console')
self.media = QMediaPlayer(None, QMediaPlayer.StreamPlayback)
self.video = QVideoWidget(self.tabs)
self.media.setVideoOutput(self.video)
self.video_reset()
exitIcn = self.style().standardIcon(QStyle.SP_FileDialogEnd)
exitAct = QAction(exitIcn, 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit MTDA')
exitAct.triggered.connect(self.exit)
powerAct = QAction('Power on/off', self)
powerAct.setShortcut('Ctrl+P')
powerAct.triggered.connect(self.power)
self.powerAct = powerAct
self.power_reset()
storageAct = QAction('Swap storage', self)
storageAct.setShortcut('Ctrl+S')
storageAct.triggered.connect(self.storage)
self.storageAct = storageAct
self.storage_reset()
captureIcn = self.style().standardIcon(QStyle.SP_DialogSaveButton)
captureAct = QAction(captureIcn, 'Capture screen start/stop', self)
captureAct.setStatusTip('Capture screen output start/stop')
captureAct.triggered.connect(self.capture)
self.captureAct = captureAct
self.capture_reset()
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
toolbar = self.addToolBar('Exit')
toolbar.addAction(powerAct)
toolbar.addAction(storageAct)
toolbar.addAction(captureAct)
self.resize(880, 760)
self.setWindowTitle('MTDA')
self.show()
def power(self):
agent = self.console.agent()
if agent is not None:
agent.target_toggle()
def power_event(self, event):
a = self.powerAct
if event == "OFF":
icn = self.style().standardIcon(QStyle.SP_ComputerIcon)
txt = "Power on device"
self.video_stop()
else:
icn = self.style().standardIcon(QStyle.SP_MediaStop)
txt = "Power off device"
self.video_start()
a.setStatusTip(txt)
a.setText(txt)
a.setIcon(icn)
def power_reset(self):
agent = self.console.agent()
enabled = (agent is not None)
# Sync UI state with agent
self.powerAct.setEnabled(enabled)
if enabled is True:
self.power_event(agent.target_status())
def service_change(self, item):
info = item.data(Qt.UserRole)
ip = socket.inet_ntoa(info.addresses[0])
# Stop on-going screen capture / video playback
self.capture_stop()
self.video_stop()
# Connect to selected remote
self.video_url = ""
self.console.execute(ip)
# Listen to screen events from the agent
screen = self.console.screen()
screen.add_listener(self.device_event)
# Reset widgets
self.power_reset()
self.capture_reset()
self.storage_reset()
self.video_reset()
def service_event(self, event, name, data=None):
if event == "ADD":
icon = self.style().standardIcon(QStyle.SP_ComputerIcon)
newService = QListWidgetItem()
shortname = name.split('.')[0]
newService.setIcon(icon)
newService.setText(shortname)
newService.setData(Qt.ToolTipRole, name)
newService.setData(Qt.UserRole, data)
self.services.addItem(newService)
else:
count = self.services.count()
for i in range(0, count):
item = self.services.item(i)
if item.data(Qt.ToolTipRole) == name:
if event == "REMOVE":
self.services.takeItem(i)
if event == "UPDATE":
item.setData(Qt.UserRole, data)
break
def storage(self):
agent = self.console.agent()
if agent is not None:
agent.storage_swap()
def storage_event(self, event):
a = self.storageAct
if event == "HOST":
icn = self.style().standardIcon(QStyle.SP_DriveHDIcon)
txt = "Connect storage to target"
elif event == "TARGET":
icn = self.style().standardIcon(QStyle.SP_DirClosedIcon)
txt = "Connect storage to host"
else:
return None
a.setStatusTip(txt)
a.setText(txt)
a.setIcon(icn)
def storage_reset(self):
agent = self.console.agent()
enabled = (agent is not None)
self.storageAct.setEnabled(enabled)
if agent is not None:
status, writing, written = agent.storage_status()
self.storage_event(status)
def video_reset(self):
agent = self.console.agent()
enabled = (agent is not None)
self.video_url = None
if enabled is True:
opts = {'sink': {'name': 'qtvideosink'}}
self.video_url = agent.video_url(opts=opts)
idx = self.tabs.indexOf(self.video)
if self.video_url is not None:
# Use provided video url
self.media.setMedia(QMediaContent(QUrl(self.video_url)))
if idx < 0:
self.tabs.addTab(self.video, 'Video')
elif idx >= 0:
# Remove Video tab
self.tabs.removeTab(idx)
def video_start(self):
if self.video_url != "":
self.media.play()
def video_stop(self):
self.media.stop()
def main():
app = QApplication(sys.argv)
MultiTenantDeviceAccessWindow()
os._exit(app.exec_())
if __name__ == '__main__':
main()