Agrarsense
AgrarsenseGameInstance.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
10
11#include "Internationalization/Regex.h"
12#include "Misc/CommandLine.h"
13#include "Engine/World.h"
14
16{
17 bool ConnectToROS = DefaultConnectToROS;
18 bool AsyncROSConnection = DefaultAsyncROSConnection;
19 FString Host = DefaultROSHost;
20 int Port = DefaultROSPort;
21 int MaxRetries = DefaultROSMaxRetries;
22
23 ParseLaunchArguments(ConnectToROS, AsyncROSConnection, Host, Port, MaxRetries);
24
25 // Set UROSIntegrationGameInstance ROS variables, afterwards these cannot be changed.
26 bConnectToROS = ConnectToROS;
27 bConnectToROSAsync = AsyncROSConnection;
28 ROSBridgeServerHost = Host;
29 ROSBridgeServerPort = Port;
30 bMaxRetries = MaxRetries;
31
32 Super::Init();
33}
34
36{
37 Super::BeginDestroy();
38}
39
41{
42 Super::Shutdown();
43}
44
46{
47 UROSHandler* ROSHandler = UAgrarsenseStatics::GetROSHandle(GetWorld());
48 if (ROSHandler)
49 {
50 SimulatorLog::Log("ROS Connected.");
51 ROSHandler->OnROSStateChanged.Broadcast(EROSState::Connected);
52 }
53}
54
56{
57 UROSHandler* ROSHandler = UAgrarsenseStatics::GetROSHandle(GetWorld());
58 if (ROSHandler)
59 {
60 SimulatorLog::Log("ROS Disconnected.");
61 ROSHandler->OnROSStateChanged.Broadcast(EROSState::Disconnected);
62 }
63}
64
65void UAgrarsenseGameInstance::ParseLaunchArguments(bool& ConnectToROS, bool& AsyncROSConnection, FString& Host, int& Port, int& MaxRetries)
66{
67 if (FParse::Param(FCommandLine::Get(), TEXT("-no-ros")))
68 {
69 ConnectToROS = false;
70 }
71
72 if (FParse::Param(FCommandLine::Get(), TEXT("-no-ros-async")))
73 {
74 AsyncROSConnection = false;
75 }
76
77 FString HostString;
78 if (FParse::Value(FCommandLine::Get(), TEXT("-ros-host-ip="), HostString))
79 {
80 FRegexPattern IpPattern(TEXT("^\\d+\\.\\d+\\.\\d+\\.\\d+$"));
81 FRegexMatcher Matcher(IpPattern, HostString);
82
83 if (Matcher.FindNext())
84 {
85 Host = HostString;
86 }
87 }
88
89 FString PortString;
90 if (FParse::Value(FCommandLine::Get(), TEXT("-ros-port="), PortString))
91 {
92 if (PortString.IsNumeric())
93 {
94 Port = FCString::Atoi(*PortString);
95 }
96 }
97
98 FString MaxRetriesString;
99 if (FParse::Value(FCommandLine::Get(), TEXT("-ros-max-retries="), MaxRetriesString))
100 {
101 if (MaxRetriesString.IsNumeric())
102 {
103 MaxRetries = FCString::Atoi(*MaxRetriesString);
104 }
105 }
106}
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
virtual void Init() override
virtual void BeginDestroy() override
void ParseLaunchArguments(bool &ConnectToROS, bool &AsyncROSConnection, FString &Host, int &Port, int &MaxRetries)
virtual void ROSBridgeDisconnected() override
virtual void ROSBridgeConnected() override
virtual void Shutdown() override
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
FROSDelegate_ROState OnROSStateChanged
Definition: ROSHandler.h:81