Skip to content

Commit

Permalink
Begin reducing dependency on ConsoleHandle in infra.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Jan 15, 2025
1 parent a4648f8 commit 7bbd96d
Show file tree
Hide file tree
Showing 117 changed files with 1,102 additions and 732 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/Environment/Environment.h"
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
#include "CommonFramework/Recording/StreamHistorySession.h"
#include "ProgramDumper.h"
#include "ErrorReports.h"

Expand Down Expand Up @@ -114,7 +114,7 @@ SendableErrorReport::SendableErrorReport(
std::string title,
std::vector<std::pair<std::string, std::string>> messages,
const ImageViewRGB32& image,
ConsoleHandle* console
const StreamHistorySession* stream_history
)
: SendableErrorReport()
{
Expand Down Expand Up @@ -147,8 +147,8 @@ SendableErrorReport::SendableErrorReport(
file.flush();
m_logs_name = ERROR_LOGS_NAME;
}
if (console){
if (console->save_stream_history(m_directory + "Video.mp4")){
if (stream_history){
if (stream_history->save(m_directory + "Video.mp4")){
m_video_name = "Video.mp4";
}
}
Expand Down Expand Up @@ -393,7 +393,7 @@ void report_error(
std::string title,
std::vector<std::pair<std::string, std::string>> messages,
const ImageViewRGB32& image,
ConsoleHandle* console,
const StreamHistorySession* stream_history,
const std::vector<std::string>& files
){
if (logger == nullptr){
Expand All @@ -407,7 +407,7 @@ void report_error(
std::move(title),
std::move(messages),
image,
console
stream_history
);

std::vector<std::string> full_file_paths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace PokemonAutomation{


class AsyncTask;
class ConsoleHandle;
class StreamHistorySession;


extern const std::string& ERROR_LOGS_NAME;
Expand Down Expand Up @@ -65,7 +65,7 @@ class SendableErrorReport{
std::string title = "",
std::vector<std::pair<std::string, std::string>> messages = {},
const ImageViewRGB32& image = ImageViewRGB32(),
ConsoleHandle* console = nullptr
const StreamHistorySession* stream_history = nullptr
);

// Deserialize from existing report.
Expand Down Expand Up @@ -111,7 +111,7 @@ void report_error(
std::string title = "",
std::vector<std::pair<std::string, std::string>> messages = {},
const ImageViewRGB32& image = ImageViewRGB32(),
ConsoleHandle* console = nullptr,
const StreamHistorySession* stream_history = nullptr,
const std::vector<std::string>& files = {}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FatalProgramException : public ScreenshotException{
: ScreenshotException(
e.m_send_error_report,
std::move(e.m_message),
e.m_console,
e.m_stream,
std::move(e.m_screenshot)
)
{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ class OperationFailedException : public ScreenshotException{
// This is the most common use case. Throw and log exception.
// Include console information for screenshot and stream history.
[[noreturn]] static void fire(
ConsoleHandle& console,
ErrorReport error_report,
std::string message
std::string message,
VideoStream& stream
){
throw_and_log<OperationFailedException>(console, error_report, std::move(message), console);
throw_and_log<OperationFailedException>(stream.logger, error_report, std::move(message), stream);
}
[[noreturn]] static void fire(
ConsoleHandle& console,
ErrorReport error_report,
std::string message,
VideoStream& stream,
std::shared_ptr<const ImageRGB32> screenshot
){
throw_and_log<OperationFailedException>(console, error_report, std::move(message), &console, std::move(screenshot));
throw_and_log<OperationFailedException>(stream.logger, error_report, std::move(message), &stream, std::move(screenshot));
}

virtual const char* name() const override{ return "OperationFailedException"; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ ProgramFinishedException::ProgramFinishedException(std::string message)

ProgramFinishedException::ProgramFinishedException(
std::string message,
ConsoleHandle& console
VideoStream& stream
)
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), console)
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), stream)
{}
ProgramFinishedException::ProgramFinishedException(
ErrorReport error_report,
std::string message,
ConsoleHandle* console,
VideoStream* stream,
ImageRGB32 screenshot
)
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), console, std::move(screenshot))
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), stream, std::move(screenshot))
{}
ProgramFinishedException::ProgramFinishedException(
std::string message,
ConsoleHandle* console,
VideoStream* stream,
std::shared_ptr<const ImageRGB32> screenshot
)
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), console, std::move(screenshot))
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), stream, std::move(screenshot))
{}

void ProgramFinishedException::log(Logger& logger) const{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ProgramFinishedException : public ScreenshotException{
// This will take a screenshot and store the console if the stream history if requested later.
explicit ProgramFinishedException(
std::string message,
ConsoleHandle& console
VideoStream& stream
);

// Construct exception with message with screenshot and (optionally) console information.
Expand All @@ -42,12 +42,12 @@ class ProgramFinishedException : public ScreenshotException{
explicit ProgramFinishedException(
ErrorReport error_report,
std::string message,
ConsoleHandle* console,
VideoStream* stream,
ImageRGB32 screenshot
);
explicit ProgramFinishedException(
std::string message,
ConsoleHandle* console,
VideoStream* stream,
std::shared_ptr<const ImageRGB32> screenshot
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,49 @@ ScreenshotException::ScreenshotException(ErrorReport error_report, std::string m
ScreenshotException::ScreenshotException(
ErrorReport error_report,
std::string message,
ConsoleHandle& console
VideoStream& stream
)
: ScreenshotException(error_report, std::move(message))
: m_send_error_report(error_report)
, m_message(std::move(message))
, m_stream(&stream)
, m_screenshot(stream.video.snapshot().frame)
{
m_console = &console;
m_screenshot = console.video().snapshot().frame;
if (m_screenshot == nullptr || !*m_screenshot){
console.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
stream.logger.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
}
}
ScreenshotException::ScreenshotException(
ErrorReport error_report,
std::string message,
ConsoleHandle* console,
VideoStream* stream,
ImageRGB32 screenshot
)
: ScreenshotException(error_report, std::move(message))
{
m_console = console;
m_screenshot = std::make_shared<ImageRGB32>(std::move(screenshot));
}
: m_send_error_report(error_report)
, m_message(std::move(message))
, m_stream(stream)
, m_screenshot(std::make_shared<ImageRGB32>(std::move(screenshot)))
{}
ScreenshotException::ScreenshotException(
ErrorReport error_report,
std::string message,
ConsoleHandle* console,
VideoStream* stream,
std::shared_ptr<const ImageRGB32> screenshot
)
: ScreenshotException(error_report, std::move(message))
{
m_console = console;
m_screenshot = std::move(screenshot);
}
: m_send_error_report(error_report)
, m_message(std::move(message))
, m_stream(stream)
, m_screenshot(std::move(screenshot))
{}


void ScreenshotException::add_console_if_needed(ConsoleHandle& console){
if (m_console == nullptr){
m_console = &console;
void ScreenshotException::add_stream_if_needed(VideoStream& stream){
if (m_stream == nullptr){
m_stream = &stream;
}
if (!m_screenshot){
m_screenshot = console.video().snapshot();
m_screenshot = stream.video.snapshot();
if (m_screenshot == nullptr || !*m_screenshot){
console.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
stream.logger.log("Camera returned empty screenshot. Is the camera frozen?", COLOR_RED);
}
}
}
Expand All @@ -94,7 +95,7 @@ void ScreenshotException::send_notification(ProgramEnvironment& env, EventNotifi
name(),
embeds,
screenshot(),
m_console
m_stream ? &m_stream->video_history : nullptr
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace PokemonAutomation{
class ImageViewRGB32;
class ImageRGB32;
class EventNotificationOption;
struct VideoStream;
struct ProgramInfo;
class ProgramEnvironment;
class ConsoleHandle;


enum class ErrorReport{
Expand All @@ -40,7 +40,7 @@ class ScreenshotException : public Exception{
explicit ScreenshotException(
ErrorReport error_report,
std::string message,
ConsoleHandle& console
VideoStream& stream
);

// Construct exception with message with screenshot and (optionally) console information.
Expand All @@ -49,19 +49,19 @@ class ScreenshotException : public Exception{
explicit ScreenshotException(
ErrorReport error_report,
std::string message,
ConsoleHandle* console,
VideoStream* stream,
ImageRGB32 screenshot
);
explicit ScreenshotException(
ErrorReport error_report,
std::string message,
ConsoleHandle* console,
VideoStream* stream,
std::shared_ptr<const ImageRGB32> screenshot
);

// Add console information if it isn't already requested.
// This will provide screenshot and stream history if requested later.
void add_console_if_needed(ConsoleHandle& console);
void add_stream_if_needed(VideoStream& stream);


public:
Expand All @@ -75,7 +75,7 @@ class ScreenshotException : public Exception{
public:
ErrorReport m_send_error_report;
std::string m_message;
ConsoleHandle* m_console = nullptr;
VideoStream* m_stream = nullptr;
std::shared_ptr<const ImageRGB32> m_screenshot;
};

Expand Down
18 changes: 10 additions & 8 deletions SerialPrograms/Source/CommonFramework/Tools/ConsoleHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,27 @@ ConsoleHandle::ConsoleHandle(
AudioFeed& audio,
const StreamHistorySession& history
)
: m_index(index)
, m_logger(logger)
: VideoStream(logger, audio, video, history)
, m_index(index)
// , m_logger(logger)
, m_botbase(botbase)
, m_video(video)
// , m_stream(logger, audio, video, history)
// , m_video(video)
, m_overlay(overlay)
, m_audio(audio)
, m_history(history)
// , m_audio(audio)
// , m_history(history)
, m_thread_utilization(new ThreadUtilizationStat(current_thread_handle(), "Program Thread:"))
{
m_overlay.add_stat(*m_thread_utilization);
}

bool ConsoleHandle::save_stream_history(const std::string& filename){
return m_history.save(filename);
return video_history.save(filename);
}

void ConsoleHandle::initialize_inference_threads(CancellableScope& scope, AsyncDispatcher& dispatcher){
m_video_pivot = std::make_unique<VisualInferencePivot>(scope, m_video, dispatcher);
m_audio_pivot = std::make_unique<AudioInferencePivot>(scope, m_audio, dispatcher);
m_video_pivot = std::make_unique<VisualInferencePivot>(scope, VideoStream::video, dispatcher);
m_audio_pivot = std::make_unique<AudioInferencePivot>(scope, VideoStream::audio, dispatcher);
m_overlay.add_stat(*m_video_pivot);
m_overlay.add_stat(*m_audio_pivot);
}
Expand Down
Loading

0 comments on commit 7bbd96d

Please sign in to comment.