FastLED 3.9.15
Loading...
Searching...
No Matches

◆ handleResponse()

void handleResponse ( const fl::json & response)

Definition at line 192 of file RpcClient.ino.

192 {
193 // Check if this is our expected response
194 if (response.contains("id")) {
195 const fl::json& idJson = response["id"];
196 if (idJson.is_number()) {
197 auto idOpt = idJson.as_int();
198 if (idOpt) {
199 int id = *idOpt;
200 if (id != expectedRequestId) {
201 fl::printf("Warning: Received response for ID %d, expected %d\n", id, expectedRequestId);
202 }
203 }
204 }
205 }
206
207 // Check for error
208 if (response.contains("error")) {
209 Serial.println("ERROR Response:");
210 Serial.println(response.to_string().c_str());
211 waitingForResponse = false;
212 return;
213 }
214
215 // Check for ACK (ASYNC/ASYNC_STREAM modes)
216 if (response.contains("result")) {
217 const fl::json& result = response["result"];
218
219 if (result.contains("ack")) {
220 auto ackOpt = result["ack"].as_bool();
221 if (ackOpt && *ackOpt == true) {
222 Serial.println("Received ACK - request accepted");
223 return; // Wait for actual result
224 }
225 }
226
227 // Check for streaming update (ASYNC_STREAM mode)
228 if (result.contains("update")) {
229 auto updateOpt = result["update"].as_int();
230 if (updateOpt) {
231 int update = *updateOpt;
232 fl::printf("Received update: %d\n", update);
233
234 // Visual feedback: show progress on LEDs
235 int ledIndex = (update % NUM_LEDS);
236 leds[ledIndex] = CRGB::Blue;
237 FastLED.show();
238 }
239 return; // Wait for more updates or final
240 }
241
242 // Check for final result (ASYNC_STREAM mode)
243 if (result.contains("stop")) {
244 auto stopOpt = result["stop"].as_bool();
245 if (stopOpt && *stopOpt == true) {
246 if (result.contains("value")) {
247 Serial.print("Received FINAL result: ");
248 Serial.println(result["value"].to_string().c_str());
249 } else {
250 Serial.println("Received FINAL marker (no value)");
251 }
252 waitingForResponse = false;
253 return;
254 }
255 }
256
257 // Regular result (SYNC or ASYNC final)
258 Serial.print("Received result: ");
259 Serial.println(result.to_string().c_str());
260 waitingForResponse = false;
261 } else {
262 Serial.println("Received response without result:");
263 Serial.println(response.to_string().c_str());
264 }
265}
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
bool waitingForResponse
int expectedRequestId
Definition RpcClient.ino:62
fl::optional< i64 > as_int() const FL_NOEXCEPT
Definition json.h:255
bool is_number() const FL_NOEXCEPT
Definition json.h:244
void to_string(const fl::u16 *bit_data, fl::u32 bit_count, string *dst)
Definition bitset.cpp.hpp:9
void printf(const char *format, const Args &... args) FL_NOEXCEPT
Printf-like formatting function that prints directly to the platform output.
Definition stdio.h:635
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
@ Blue
<div style='background:#0000FF;width:4em;height:4em;'></div>
Definition crgb.h:512
#define Serial
Definition serial.h:304

References fl::json::as_int(), fl::CRGB::Blue, expectedRequestId, FastLED, fl::json::is_number(), leds, NUM_LEDS, fl::printf(), Serial, and waitingForResponse.

Referenced by loop().

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