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 3:
78 case 4:
79 case 5:
80 return ESeason::Spring;
81
82 case 6:
83 case 7:
84 case 8:
85 return ESeason::Summer;
86
87 case 9:
88 case 10:
89 case 11:
90 return ESeason::Autumn;
91
92 default:
93 return ESeason::Winter;
94 }
95}
96
97void AWeather::UpdateWeather(const FWeatherParameters& InWeather, bool updateToROS)
98{
99 CurrentWeather = InWeather;
100
101 if (updateToROS)
102 {
103 AsyncTask(ENamedThreads::AnyThread, [this]()
104 {
105 // Send weather parameters to ROS, we don't care if this message will be frame or two behind,
106 // as long as it doesn't block the game thread.
108 });
109 }
110
111 // Broadcast event that weather has been changed
113}
114
115FString AWeather::ExportToJSON(const FString& FileName)
116{
118}
119
121{
123 {
124 return;
125 }
126
127 // Else create Weather ROS topic
128 WeatherTopic = NewObject<UTopic>(UTopic::StaticClass());
129 const FString TopicName = "/agrarsense/out/weather/";
130 WeatherTopic->Init(RosInstance->ROSIntegrationCore, TopicName, TEXT("std_msgs/String"));
131 WeatherTopic->Advertise();
132
133#if WITH_EDITOR
134 UE_LOG(LogTemp, Warning, TEXT("Weather.cpp: Created ROS topic. Topic name: %s"), *TopicName);
135#endif
136}
137
139{
140 if (WeatherTopic && ROSConnected && Message.IsValid())
141 {
142 Message->_Data = Parameters.ToStringHumanReadable();
143 WeatherTopic->Publish(Message);
144 }
145}
146
148{
149 if (WeatherTopic)
150 {
151 WeatherTopic->Unadvertise();
152 WeatherTopic->Unsubscribe();
153 WeatherTopic->MarkAsDisconnected();
154 WeatherTopic->ConditionalBeginDestroy();
155 WeatherTopic = nullptr;
156 }
157}
158
160{
161 switch (ROSState)
162 {
164 ROSConnected = true;
166 break;
167
169 ROSConnected = false;
170 DestroyTopic();
171 break;
172 }
173}
EROSState
Definition: ROSState.h:16
ESeason
Definition: Season.h:12
void ROSBridgeStateChanged(EROSState ROSState)
Definition: Weather.cpp:159
ESeason GetCurrentSeason()
Definition: Weather.cpp:68
void DestroyTopic()
Definition: Weather.cpp:147
void SendWeatherData(FWeatherParameters Parameters)
Definition: Weather.cpp:138
void UpdateWeather(const FWeatherParameters &WeatherParameters, bool updateToROS)
Definition: Weather.cpp:97
void InitializeROSTopic()
Definition: Weather.cpp:120
bool ROSConnected
Definition: Weather.h:112
UTopic * WeatherTopic
Definition: Weather.h:107
ESeason GetSeasonForMonth(const int Month)
Definition: Weather.cpp:73
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:115
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