So I tried with bad imports and fake methods and those do throw errors. But trying to call a method that I did not import does not throw any error, nor leave a trace in the log. For instance, accidentally importing ByteArrayOutputStream when I actually want/call ByteArrayInputStream
#write_image handler
def handleMessage(payload):
import base64
from java.io import File, ByteArrayInputStream # If this is ByteArrayOutputStream it just dies at the position marked below
from javax.imageio import ImageIO
try:
logger = system.util.getLogger("test")
path = payload['path']
image = payload['image']
ext = payload['ext']
logger.info("Writing image to {}: {}".format(path, len(str(image))))
# Decode the base64 image back into bytes
image_data = base64.b64decode(image)
logger.info("Image data <{}> {}".format(type(image_data), len(str(image_data))))
byte_stream = ByteArrayInputStream(image_data)
## DIES HERE If importing ByteArrayOutputStream instead, we never see anything below this
logger.info("byte_stream? <{}>: {}".format(type(byte_stream ), len(str(byte_stream))))
# Read the image from the byte stream
image = ImageIO.read(byte_stream)
logger.info("image? <{}>: {}".format(type(image), len(str(image))))
logger.info("Writing byte stream to {}".format(path))
# Save image to the specified path
ImageIO.write(image, ext, File(path))
return path
except Exception as e:
return {'error': str(e)}
#gateway.py
def write_image(path, image, ext="png", project="base_leaf"):
return system.util.sendRequest(project, "write_image", {"path": path, "image": image, "ext": ext})
Bunch of trash as I try to make screenshots work right (grabbing windows is inconsistent, locating the rectangle correctly doesn't work right, there's a bunch of annoying all piled in here)
#gui.py
def get_screenshot(run_name, shot_number=None, window=None, project="tb1", ext='png'):
# Import necessary libraries
from java.awt import Robot, Rectangle
from java.awt import Toolkit
from javax.imageio import ImageIO
from java.io import File, ByteArrayOutputStream # Add this import
import system
import os
import base64
# Define the path to save the screenshot
file_dir = svc.common.work_dir(project=project, name=run_name, fs='ignition')
file_name = "{}_shot{}_allctrls".format(run_name, shot_number)
file_path = file_dir + "/" + file_name + "." + ext # assume linux since this goes to gateway
print(file_dir)
print(gateway.ensure_dir(file_dir))
print(file_path)
# parent_window = system.gui.getParentWindow()
parent_window = system.gui.getWindow("Main Windows/all_controls")
vision_client = system.gui.getParentWindow(system.gui.getWindows()[0].getRootContainer())
if window:
print("Using handled window")
window_bounds = window.getBounds()
rectangle = Rectangle(window_bounds.x, window_bounds.y, window_bounds.width, window_bounds.height)
elif system.gui.getWindows():
# Take the bounds from the first window in the list, assuming it's the main Vision client window
vision_client_window = system.gui.getWindows()[0]
window_bounds = vision_client_window.getBounds()
rectangle = Rectangle(window_bounds.x, window_bounds.y, window_bounds.width, window_bounds.height)
elif parent_window:
# Get the bounds of the Vision window (x, y, width, height)
print("Got parent window Main Windows/all_controls")
window_bounds = parent_window.getBounds()
rectangle = Rectangle(window_bounds.x, window_bounds.y, window_bounds.width, window_bounds.height)
else:
# Fall back to the whole screen if the Vision window can't be found
print("Failed to find specified window, taking full screenshot")
screen_size = Toolkit.getDefaultToolkit().getScreenSize()
rectangle = Rectangle(screen_size)
# Capture screenshot
robot = Robot()
screenshot = robot.createScreenCapture(rectangle)
byte_stream = ByteArrayOutputStream()
ImageIO.write(screenshot, ext, byte_stream)
image_bytes = byte_stream.toByteArray()
encoded_image = base64.b64encode(image_bytes).decode('utf-8')
gateway.write_image(file_path, encoded_image, ext=ext)
# # Save screenshot
# ImageIO.write(screenshot, ext, File(file_path))
# Notify the user
# system.gui.messageBox("Screenshot saved at: " + file_path, "Screenshot Taken")
return file_path