Agrarsense
Weather.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
7
11
12#include "ROSIntegration/Classes/ROSIntegrationGameInstance.h"
13#include "RI/Topic.h"
14
15#include "Async/Async.h"
16
17AWeather::AWeather(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
18{
19 PrimaryActorTick.bCanEverTick = false;
20}
21
23{
24 Super::BeginPlay();
25
27
28 UWorld* World = GetWorld();
29
31 if (RosHandler)
32 {
33 RosHandler->OnROSStateChanged.AddUniqueDynamic(this, &AWeather::ROSBridgeStateChanged);
34 }
35
36 RosInstance = Cast<UROSIntegrationGameInstance>(World->GetGameInstance());
37 if (RosInstance)
38 {
39 ROSConnected = RosInstance->IsROSConnected();
40 }
41
42 Message = MakeShared<ROSMessages::std_msgs::String>();
43
45}
46
47void AWeather::EndPlay(const EEndPlayReason::Type EndPlayReason)
48{
49 Super::EndPlay(EndPlayReason);
50
51 UROSHandler* RosHandler = UAgrarsenseStatics::GetROSHandle(GetWorld());
52 if (RosHandler)
53 {
54 RosHandler->OnROSStateChanged.AddUniqueDynamic(this, &AWeather::ROSBridgeStateChanged);
55 }
56
57 Message.Reset();
59 RosInstance = nullptr;
60}
61
63{
66}
67
69{
71}
72
74{
75 switch (Month)
76 {
77 case 0:
78 case 1:
79 case 2:
80 case 3:
81 case 12:
82 return ESeason::Winter;
83
84 case 4:
85 case 5:
86 return ESeason::Spring;
87
88 case 6:
89 case 7:
90 case 8:
91 return ESeason::Summer;
92
93 case 9:
94 case 10:
95 case 11:
96 return ESeason::Autumn;
97
98 default:
99 return ESeason::Summer;
100 }
101}
102
103void AWeather::UpdateWeather(const FWeatherParameters& InWeather, bool updateToROS)
104{
105 CurrentWeather = InWeather;
106
107 if (updateToROS)
108 {
109 AsyncTask(ENamedThreads::AnyThread, [this]()
110 {
111 // Send weather parameters to ROS, we don't care if this message will be frame or two behind,
112 // as long as it doesn't block the game thread.
114 });
115 }
116
117 // Broadcast event that weather has been changed
119}
120
121FString AWeather::ExportToJSON(const FString& FileName)
122{
124}
125
127{
129 {
130 return;
131 }
132
133 // Else create Weather ROS topic
134 WeatherTopic = NewObject<UTopic>(UTopic::StaticClass());
135 const FString TopicName = "/agrarsense/out/weather/";
136 WeatherTopic->Init(RosInstance->ROSIntegrationCore, TopicName, TEXT("std_msgs/String"));
137 WeatherTopic->Advertise();
138
139#if WITH_EDITOR
140 UE_LOG(LogTemp, Warning, TEXT("Weather.cpp: Created ROS topic. Topic name: %s"), *TopicName);
141#endif
142}
143
145{
146 if (WeatherTopic && ROSConnected && Message.IsValid())
147 {
148 Message->_Data = Parameters.ToStringHumanReadable();
149 WeatherTopic->Publish(Message);
150 }
151}
152
154{
155 if (WeatherTopic)
156 {
157 WeatherTopic->Unadvertise();
158 WeatherTopic->Unsubscribe();
159 WeatherTopic->MarkAsDisconnected();
160 WeatherTopic->ConditionalBeginDestroy();
161 WeatherTopic = nullptr;
162 }
163}
164
166{
167 switch (ROSState)
168 {
170 ROSConnected = true;
172 break;
173
175 ROSConnected = false;
176 DestroyTopic();
177 break;
178 }
179}
EROSState
Definition: ROSState.h:16
ESeason
Definition: Season.h:12
void ROSBridgeStateChanged(EROSState ROSState)
Definition: Weather.cpp:165
ESeason GetCurrentSeason()
Definition: Weather.cpp:68
void DestroyTopic()
Definition: Weather.cpp:153
void SendWeatherData(FWeatherParameters Parameters)
Definition: Weather.cpp:144
void UpdateWeather(const FWeatherParameters &WeatherParameters, bool updateToROS)
Definition: Weather.cpp:103
ESeason GetSeasonForMonth(const int32 Month)
Definition: Weather.cpp:73
void InitializeROSTopic()
Definition: Weather.cpp:126
bool ROSConnected
Definition: Weather.h:112
UTopic * WeatherTopic
Definition: Weather.h:107
AWeather(const FObjectInitializer &ObjectInitializer)
Definition: Weather.cpp:17
const FWeatherParameters Default
Definition: Weather.h:119
void ResetWeather()
Definition: Weather.cpp:62
FString ExportToJSON(const FString &FileName)
Definition: Weather.cpp:121
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: Weather.cpp:47
TSharedPtr< ROSMessages::std_msgs::String > Message
Definition: Weather.h:117
FLevelEventDelegate_WeatherChanged OnWeatherChanged
Definition: Weather.h:85
UROSIntegrationGameInstance * RosInstance
Definition: Weather.h:110
FWeatherParameters CurrentWeather
Definition: Weather.h:104
virtual void BeginPlay() override
Definition: Weather.cpp:22
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
FROSDelegate_ROState OnROSStateChanged
Definition: ROSHandler.h:81
static FString ExportWeatherToJSON(const FString &FileName, const FWeatherParameters &WeatherParameters)
FString ToStringHumanReadable() const