Skip to content

Commit ef60c87

Browse files
author
Shivaschandra KL
committed
feat: capture screenshot from mtda
Add feature to capture screenshots from HDMI streamer on MTDA. Signed-off-by: Shivaschandra KL <shivaschandra.k-l@siemens.com>
1 parent 2696d7e commit ef60c87

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

mtda-cli

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import time
1717
import sys
1818
import socket
1919
from argparse import ArgumentParser, RawTextHelpFormatter
20+
from pathlib import Path
2021

2122
# Local imports
2223
from mtda.main import MultiTenantDeviceAccess
@@ -88,6 +89,27 @@ class Application:
8889

8990
def debug(self, level, msg):
9091
self.client().debug(level, msg)
92+
93+
def capture_screen(self,args):
94+
file_name = args.capture_file_name
95+
file_path = Path(args.capture_file_name)
96+
parent_dir = file_path.parent
97+
valid_extensions = ('.png', '.jpg', '.jpeg')
98+
if not file_name.lower().endswith(valid_extensions):
99+
print(f"'{input_string}' is not a valid image path (must end in .png ,.jpg or .jpeg).")
100+
return False
101+
if parent_dir.exists() and parent_dir.is_dir():
102+
print(f"Path leading to '{file_path.name}' is valid. Proceeding with capture...")
103+
else:
104+
print(f"Error: The directory '{parent_dir}' does not exist.")
105+
return False
106+
response = self.client().capture_screen()
107+
if response is not None:
108+
with open(file_name, "wb") as f:
109+
f.write(response)
110+
print(f"Captured image saved at {file_name}")
111+
else:
112+
print("Failed to capture image")
91113

92114
def command_cmd(self, args):
93115
result = self.client().command(args.cmd_command)
@@ -612,6 +634,20 @@ class Application:
612634
"cmd_command", metavar="cmd", type=str, help="Command to send"
613635
)
614636
p.set_defaults(func=cmd)
637+
638+
# subcommand: capture
639+
cmd = self.capture_screen
640+
p = subparsers.add_parser(
641+
"capture",
642+
help="This utility helps you to capture screenshot of current diplay on monitor.\n"
643+
)
644+
p.add_argument(
645+
"capture_file_name",
646+
metavar="capture_file_name",
647+
type=str,
648+
help="Filename to store the captured image, .png or .jpg"
649+
)
650+
p.set_defaults(func=cmd)
615651

616652
# subcommand: console
617653
cmd = self.console_cmd

mtda/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def wrapper(*args, **kwargs):
8282
return wrapper
8383
return attr
8484

85+
def capture_screen(self, filename):
86+
return self._impl.capture_screen()
87+
8588
def console_prefix_key(self):
8689
return self._agent.console_prefix_key()
8790

mtda/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ def _composite_stop(self):
181181
self.mtda.debug(3, f"main._composite_stop(): {result}")
182182
return result
183183

184+
@Pyro4.expose
185+
def capture_screen(self):
186+
self.mtda.debug(3, f"main.capture_screen()")
187+
result = None
188+
if self.video is not None:
189+
if self.video.variant == "mjpg_streamer":
190+
url = self.video.url() + "/?action=snapshot"
191+
self.mtda.debug(3, f"main.capture_screen(): {url} ")
192+
try:
193+
result = requests.get(url, timeout=5)
194+
result.raise_for_status()
195+
return result.content
196+
except Exception as e:
197+
self.mtda.debug(3, f"main.captue screenshot(): {e} ")
198+
self.mtda.debug(3, f"main.captue screenshot(): {result} ")
199+
return result
200+
184201
@Pyro4.expose
185202
def config_set_power_timeout(self, timeout, **kwargs):
186203
self.mtda.debug(3, "main.config_set_power_timeout()")
@@ -1560,6 +1577,8 @@ def video_url(self, host="", opts=None, **kwargs):
15601577
self.session_ping(session)
15611578
if self.video is not None:
15621579
result = self.video.url(host, opts)
1580+
if self.video.variant == "mjpg_streamer":
1581+
result = result + "/?action=stream"
15631582

15641583
self.mtda.debug(3, f"main.video_url(): {result}")
15651584
return result

mtda/video/mjpg_streamer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def url(self, host="", opts=None):
154154
host = socket.getfqdn()
155155
self.mtda.debug(3, "video.mjpg_streamer."
156156
f"url: using host='{str(host)}'")
157-
result = f"http://{host}:{self.port}/?action=stream"
157+
result = f"http://{host}:{self.port}"
158158

159159
self.mtda.debug(3, f"video.mjpg_streamer.url(): {str(result)}")
160160
return result

0 commit comments

Comments
 (0)