Noah b3e0389675 Created image write on async thread
2025-05-01 17:33:18 +00:00

133 lines
3.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "IWebSocket.h"
#include "LuckySensorPawnBase.h"
#include "ObservationData.h"
#include "Subsystems/WorldSubsystem.h"
#include "LuckyDataTransferSubsystem.generated.h"
/**
*
*/
USTRUCT(BlueprintType)
struct FCommand
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly, Category = "Command")
FString Key = FString();
UPROPERTY(BlueprintReadOnly, Category = "Command")
float Value = 0.f;
};
USTRUCT(BlueprintType)
struct FPayload
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly, Category = "Command")
TArray<FCommand> Commands;
UPROPERTY(BlueprintReadOnly, Category = "Command")
int32 Index = 0;
};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FCommandReady, const FPayload&, Payload);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSocketReady, bool, bSuccessful);
UCLASS()
class LUCKYDATATRANSFER_API ULuckyDataTransferSubsystem : public UWorldSubsystem
{
GENERATED_BODY()
public:
ULuckyDataTransferSubsystem();
virtual void Initialize(FSubsystemCollectionBase& Collection);
virtual void Deinitialize();
TSharedPtr<IWebSocket> Socket;
//internal references
void Internal_OpenWebsocket(const FString& URL, const FString& Protocol);
FPayload InterpretData(const FString& Message);
UPROPERTY(BlueprintAssignable)
FCommandReady OnCommandReady;
UPROPERTY(BlueprintAssignable)
FSocketReady OnSocketReady;
//Callbacks
UFUNCTION()
void Callback_OnConnected();
UFUNCTION()
void Callback_OnConnectionError(const FString& Error);
UFUNCTION()
void Callback_OnMessage(const FString& Message);
UFUNCTION()
void Internal_OnMessageSent(const FString& Message);
UFUNCTION()
void Callback_OnConnectionClosed();
//Exposed Blueprint Functions
UFUNCTION(BlueprintCallable, Category = "Websocket")
void ConnectToWebsocket(const FString& URL, const FString& Protocol);
UFUNCTION(BlueprintCallable, Category = "Websocket")
void SendMessage(const FString& Message);
UFUNCTION()
void CommandReady(const FPayload& Payload);
//---Observations (Sent to server from Unreal)--------------//
//Feature Data declarations
UPROPERTY(BlueprintReadWrite, Category = "Observation")
FString ObservationPayloadString = FString();
//Internal Functions
bool CreateJsonPayload_Observation(const FObservationPayload& Data);
//Blueprint Callable Functions
UFUNCTION(BlueprintCallable, Category = "Websocket")
bool MakeObservationPayload(const FObservationPayload& Data);
//---------------------------------------------------------//
//---Image Capture----------------------------------------//
UFUNCTION(BlueprintCallable, Category = "Websocket")
FString CreateCaptureSessionID();
UPROPERTY(BlueprintReadOnly, Category = "Websocket")
FString SessionID;
UPROPERTY(BlueprintReadWrite, Category = "Capture")
TMap<FString, ACameraActor*> ActiveCameras;
UPROPERTY(BlueprintReadWrite, Category = "Capture")
TArray<ALuckySensorPawnBase*> SensorPawns;
UFUNCTION(BlueprintCallable, Category = "Capture")
void RegisterSensor(ALuckySensorPawnBase* Sensor);
UFUNCTION(BlueprintCallable, Meta = (AutoCreateRefTerm = "Path, Comment"), Category = "Capture")
bool WriteImageToDisk(const FString& Path, const double Timestamp, const FString& Comment);
//-------------------------------------------------------//
};