Magic Poi Lite 0.1
IOT LED Poi
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
Loading Class Reference
Collaboration diagram for Loading:
Collaboration graph

Public Member Functions

 Loading ()
 Default constructor for Loading class.
 
String getTimelineNumber ()
 Retrieves the current timeline number from the server.
 
bool getTimeline (String tln)
 Retrieves the timeline data from the server based on the specified timeline number.
 
void saveTimeline (const String &timelineData)
 Saves the timeline data to a file.
 
bool load ()
 Loads data required for the application.
 

Private Attributes

WiFiClient client
 WiFi client object.
 
Authentication authentication
 Authentication object.
 
char token [512]
 JWT token buffer.
 
String timelineNumber = "0"
 Default timeline number.
 
String timelineFilePath = "/timeline" + timelineNumber + ".txt"
 File path of the timeline file.
 

Constructor & Destructor Documentation

◆ Loading()

Loading::Loading ( )

Default constructor for Loading class.

This constructor initializes the Loading object. It attempts to load a JWT token from a file using the Authentication object. If a token is successfully loaded, it is stored internally. Otherwise, it prepares to use password login if token loading fails.

26{
27 // token = authentication.readJWTTokenFromFile(); //todo: add logic to use Password login if this fails
28 String savedToken = authentication.readJWTTokenFromFile();
29 if (!savedToken.isEmpty())
30 {
31 strncpy(token, savedToken.c_str(), sizeof(token) - 1);
32 token[sizeof(token) - 1] = '\0'; // Null-terminate the token string
33 // strncpy(jwtToken, savedToken.c_str(), sizeof(jwtToken) - 1);
34 // jwtToken[sizeof(jwtToken) - 1] = '\0'; // Null-terminate the token string
35 // gotToken = true;
36 Serial.println("Using saved JWT token:");
37 Serial.println(token);
38 }
39}
String readJWTTokenFromFile()
Reads JWT token from a file stored on LittleFS.
Definition Authentication.cpp:39
Authentication authentication
Authentication object.
Definition Loading.h:34
char token[512]
JWT token buffer.
Definition Loading.h:41

References authentication, Authentication::readJWTTokenFromFile(), and token.

Here is the call graph for this function:

Member Function Documentation

◆ getTimeline()

bool Loading::getTimeline ( String tln)

Retrieves the timeline data from the server based on the specified timeline number.

This method sends a GET request to the server to retrieve the timeline data corresponding to the provided timeline number. It includes the JWT token in the request headers for authentication. If the request is successful and the response code is OK, the timeline data is saved and the method returns true. If any error occurs during the HTTP request or the response code indicates a failure, the method returns false.

Parameters
tlnThe timeline number to retrieve data for.
Returns
true if the timeline data is successfully retrieved, false otherwise.
102{
103 HTTPClient http;
104 http.begin(client, "http://" + String(serverIP) + ":" + String(serverPort) + "/lite/api/load-timeline?number=" + tln);
105 http.addHeader("Authorization", "Bearer " + String(token));
106
107 Serial.println("[HTTP] GET...");
108 int httpCode = http.GET();
109 // httpCode will be negative on error
110 if (httpCode > 0)
111 {
112 if (httpCode == HTTP_CODE_OK)
113 {
114 // Print the API response
115 // DynamicJsonDocument led_doc(1500);
116 String payload = http.getString();
117 saveTimeline(payload);
118 Serial.println("got Timeline: ");
119 Serial.println(payload);
120 // delay(10);
121 // loadTimeline();
122
123 // already_got_data = true;
124 // digitalWrite(led, LOW);
125 http.end();
126 return true;
127 }
128 else
129 {
130 Serial.print("Failed to get Timeline - Error Code: ");
131 Serial.println(httpCode);
132 // gotToken=false;
133 }
134 }
135 else
136 {
137 Serial.println("Connection failed.");
138 // gotToken=false;
139 }
140
141 http.end();
142 return false;
143}
WiFiClient client
WiFi client object.
Definition Loading.h:27
void saveTimeline(const String &timelineData)
Saves the timeline data to a file.
Definition Loading.cpp:155

References client, saveTimeline(), and token.

Referenced by load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getTimelineNumber()

String Loading::getTimelineNumber ( )

Retrieves the current timeline number from the server.

This method sends a GET request to the server to retrieve the current timeline number. It includes the JWT token in the request headers for authentication. If the request is successful and the response code is OK, the method returns the timeline number received from the server. If any error occurs during the HTTP request or the response code indicates a failure, an empty string is returned.

Returns
A string containing the current timeline number, or an empty string if the request fails or if the server response code is not OK.
55{
56 HTTPClient http;
57 http.begin(client, "http://" + String(serverIP) + ":" + String(serverPort) + "/lite/api/get-current-timeline-number");
58 http.addHeader("Authorization", "Bearer " + String(token));
59
60 Serial.println("[HTTP] GET...");
61 int httpCode = http.GET();
62 String response = "";
63 // httpCode will be negative on error
64 if (httpCode > 0)
65 {
66 if (httpCode == HTTP_CODE_OK)
67 {
68 // Print the API response
69 response = http.getString();
70 Serial.print("Got timeline number: ");
71 Serial.println(response);
72 }
73 else
74 {
75 Serial.print("Failed to get Timeline Number - Error code: ");
76 Serial.println(httpCode);
77 }
78 }
79 else
80 {
81 Serial.println("Connection failed.");
82 }
83
84 http.end();
85 return response;
86}

References client, and token.

Referenced by load().

Here is the caller graph for this function:

◆ load()

bool Loading::load ( )

Loads data required for the application.

This method attempts to load data required for the application. It first tries to retrieve the current timeline number from the server. If successful, it proceeds to load the timeline data. If both operations succeed, the method returns true indicating successful loading. If any of the operations fail, the method returns false.

Returns
true if data loading is successful, false otherwise.
182{
183 //todo: load saved timeline if not able to access internet or auth doesn't work?
184 Serial.println("Loading data...");
185
186 //load from api:
189 return true;
190 }
191
192 return false;
193
194}
String timelineNumber
Default timeline number.
Definition Loading.h:48
String getTimelineNumber()
Retrieves the current timeline number from the server.
Definition Loading.cpp:54
bool getTimeline(String tln)
Retrieves the timeline data from the server based on the specified timeline number.
Definition Loading.cpp:101

References getTimeline(), getTimelineNumber(), and timelineNumber.

Referenced by handleAuthenticationAndLoading().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ saveTimeline()

void Loading::saveTimeline ( const String & timelineData)

Saves the timeline data to a file.

This method saves the provided timeline data to a file using the LittleFS (Little File System). If LittleFS initialization succeeds and the file is successfully created and written, a success message is printed to the Serial monitor. If there's any failure during the file system operation, the method returns without saving the data.

Parameters
timelineDataThe timeline data to be saved to the file.
156{
157 if (LittleFS.begin())
158 {
159 File file = LittleFS.open(timelineFilePath, "w");
160 if (file)
161 {
162 file.print(timelineData);
163 file.close();
164 Serial.println("Timeline data saved to file.");
165 }
166 LittleFS.end();
167 }
168}
String timelineFilePath
File path of the timeline file.
Definition Loading.h:55

References timelineFilePath.

Referenced by getTimeline().

Here is the caller graph for this function:

Member Data Documentation

◆ authentication

Authentication Loading::authentication
private

Authentication object.

This object is used for handling authentication operations.

Referenced by Loading().

◆ client

WiFiClient Loading::client
private

WiFi client object.

This object is used for network communication with the server.

Referenced by getTimeline(), and getTimelineNumber().

◆ timelineFilePath

String Loading::timelineFilePath = "/timeline" + timelineNumber + ".txt"
private

File path of the timeline file.

This string represents the file path of the timeline data file, saved in LittleFS.

Referenced by saveTimeline().

◆ timelineNumber

String Loading::timelineNumber = "0"
private

Default timeline number.

This string represents the default timeline number used for file paths.

Referenced by load().

◆ token

char Loading::token[512]
private

JWT token buffer.

This buffer stores the JWT token retrieved from authentication.

Referenced by getTimeline(), getTimelineNumber(), and Loading().


The documentation for this class was generated from the following files: