forked from LuckyRobots/LuckyWorldV2
- Websocket - Json conversion both ways - Unreal Data Ustructs setup for Evan - Created data templates with ethan and anuj for the python server - Created blueprint implementation and example in Test Level
119 lines
2.7 KiB
C++
119 lines
2.7 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "IWebSocket.h"
|
|
//#include "LuckyWriteThread.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);
|
|
|
|
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;
|
|
|
|
|
|
//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)--------------//
|
|
//Image Data declarations
|
|
TArray<FString> FeatureNames = {};
|
|
FString CameraName = TEXT("front_camera");
|
|
int32 ImageHeight = 480;
|
|
int32 ImageWidth = 640;
|
|
int32 Fps = 30;
|
|
bool bUseVideo = false;
|
|
|
|
//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);
|
|
//---------------------------------------------------------//
|
|
|
|
protected:
|
|
//LuckyWriteThread* WriteThread = nullptr;
|
|
};
|