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 UROSHandler* ROSHandler = UAgrarsenseStatics::GetROSHandle(GetWorld());
43 if (ROSHandler)
44 {
45 SimulatorLog::Log("ROS Connected.");
46 ROSHandler->OnROSStateChanged.Broadcast(EROSState::Connected);
47 }
48}
49
51{
52 UROSHandler* ROSHandler = UAgrarsenseStatics::GetROSHandle(GetWorld());
53 if (ROSHandler)
54 {
55 SimulatorLog::Log("ROS Disconnected.");
56 ROSHandler->OnROSStateChanged.Broadcast(EROSState::Disconnected);
57 }
58}
59
60void UAgrarsenseGameInstance::ParseLaunchArguments(bool& ConnectToROS, bool& AsyncROSConnection, FString& Host, int& Port, int& MaxRetries)
61{
62 if (FParse::Param(FCommandLine::Get(), TEXT("-no-ros")))
63 {
64 ConnectToROS = false;
65 }
66
67 if (FParse::Param(FCommandLine::Get(), TEXT("-no-ros-async")))
68 {
69 AsyncROSConnection = false;
70 }
71
72 FString HostString;
73 if (FParse::Value(FCommandLine::Get(), TEXT("-ros-host-ip="), HostString))
74 {
75 FRegexPattern IpPattern(TEXT("^\\d+\\.\\d+\\.\\d+\\.\\d+$"));
76 FRegexMatcher Matcher(IpPattern, HostString);
77
78 if (Matcher.FindNext())
79 {
80 Host = HostString;
81 }
82 }
83
84 FString PortString;
85 if (FParse::Value(FCommandLine::Get(), TEXT("-ros-port="), PortString))
86 {
87 if (PortString.IsNumeric())
88 {
89 Port = FCString::Atoi(*PortString);
90 }
91 }
92
93 FString MaxRetriesString;
94 if (FParse::Value(FCommandLine::Get(), TEXT("-ros-max-retries="), MaxRetriesString))
95 {
96 if (MaxRetriesString.IsNumeric())
97 {
98 MaxRetries = FCString::Atoi(*MaxRetriesString);
99 }
100 }
101}
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
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
FROSDelegate_ROState OnROSStateChanged
Definition: ROSHandler.h:81