@@ -512,26 +512,11 @@ void Meters::register_urls()
512512 if (!sb.setCapacity (HISTORY_JSON_SIZE )) {
513513 return request.send_plain (500 , " Failed to allocate buffer" );
514514 }
515-
516- sb.printf (" {\" offset\" :%lu,\" samples\" :[" , (now_us () - last_history_update).to <millis_t >().as <uint32_t >());
517515 request.beginChunkedResponse_json (200 );
518516
519- for (uint32_t slot = 0 ; slot < OPTIONS_METERS_MAX_SLOTS (); slot++) {
520- MeterSlot &meter_slot = meter_slots[slot];
521-
522- if (meter_slot.meter ->get_class () != MeterClassID::None) {
523- sb.puts (slot == 0 ? " [" : " ,[" );
524- meter_slot.power_history .format_history_samples (&sb);
525- sb.puts (" ]" );
526- } else {
527- sb.puts (slot == 0 ? " null" : " ,null" );
528- }
529-
530- SEND_CHUNK_OR_FAIL (request, sb);
531- sb.clear ();
532- }
533-
534- SEND_CHUNK_OR_FAIL_LEN (request, " ]}" , 2 );
517+ auto result = this ->send_meters_history (request, sb);
518+ if (result.error != ESP_OK )
519+ return result;
535520
536521 return request.endChunkedResponse ();
537522 });
@@ -543,25 +528,12 @@ void Meters::register_urls()
543528 return request.send_plain (500 , " Failed to allocate buffer" );
544529 }
545530
546- sb.printf (" {\" offset\" :%lu,\" samples_per_second\" :%f,\" samples\" :[" , (now_us () - last_live_update).to <millis_t >().as <uint32_t >(), static_cast <double >(live_samples_per_second ()));
547531 request.beginChunkedResponse_json (200 );
548532
549- for (uint32_t slot = 0 ; slot < OPTIONS_METERS_MAX_SLOTS (); slot++) {
550- MeterSlot &meter_slot = meter_slots[slot];
551-
552- if (meter_slot.meter ->get_class () != MeterClassID::None) {
553- sb.puts (slot == 0 ? " [" : " ,[" );
554- meter_slot.power_history .format_live_samples (&sb);
555- sb.puts (" ]" );
556- } else {
557- sb.puts (slot == 0 ? " null" : " ,null" );
558- }
533+ auto result = this ->send_meters_live (request, sb);
534+ if (result.error != ESP_OK )
535+ return result;
559536
560- SEND_CHUNK_OR_FAIL (request, sb);
561- sb.clear ();
562- }
563-
564- SEND_CHUNK_OR_FAIL_LEN (request, " ]}" , 2 );
565537
566538 return request.endChunkedResponse ();
567539 });
@@ -1415,6 +1387,54 @@ String Meters::get_path(uint32_t slot, Meters::PathType path_type)
14151387 return String (buf, sw.getLength ());
14161388}
14171389
1390+ WebServerRequestReturnProtect Meters::send_meters_live (WebServerRequest request, StringWriter &sw)
1391+ {
1392+ sw.printf (" {\" offset\" :%lu,\" samples_per_second\" :%f,\" samples\" :[" , (now_us () - last_live_update).to <millis_t >().as <uint32_t >(), static_cast <double >(live_samples_per_second ()));
1393+
1394+ for (uint32_t slot = 0 ; slot < OPTIONS_METERS_MAX_SLOTS (); slot++) {
1395+ MeterSlot &meter_slot = meter_slots[slot];
1396+
1397+ if (meter_slot.meter ->get_class () != MeterClassID::None) {
1398+ sw.puts (slot == 0 ? " [" : " ,[" );
1399+ meter_slot.power_history .format_live_samples (&sw);
1400+ sw.puts (" ]" );
1401+ } else {
1402+ sw.puts (slot == 0 ? " null" : " ,null" );
1403+ }
1404+
1405+ SEND_CHUNK_OR_FAIL (request, sw);
1406+ sw.clear ();
1407+ }
1408+
1409+ SEND_CHUNK_OR_FAIL_LEN (request, " ]}" , 2 );
1410+
1411+ return request.unsafe_ResponseAlreadySent ();
1412+ }
1413+
1414+ WebServerRequestReturnProtect Meters::send_meters_history (WebServerRequest request, StringWriter &sw)
1415+ {
1416+ sw.printf (" {\" offset\" :%lu,\" samples\" :[" , (now_us () - last_history_update).to <millis_t >().as <uint32_t >());
1417+
1418+ for (uint32_t slot = 0 ; slot < OPTIONS_METERS_MAX_SLOTS (); slot++) {
1419+ MeterSlot &meter_slot = meter_slots[slot];
1420+
1421+ if (meter_slot.meter ->get_class () != MeterClassID::None) {
1422+ sw.puts (slot == 0 ? " [" : " ,[" );
1423+ meter_slot.power_history .format_history_samples (&sw);
1424+ sw.puts (" ]" );
1425+ } else {
1426+ sw.puts (slot == 0 ? " null" : " ,null" );
1427+ }
1428+
1429+ SEND_CHUNK_OR_FAIL (request, sw);
1430+ sw.clear ();
1431+ }
1432+
1433+ SEND_CHUNK_OR_FAIL_LEN (request, " ]}" , 2 );
1434+
1435+ return request.unsafe_ResponseAlreadySent ();
1436+ }
1437+
14181438uint32_t meters_find_id_index (const MeterValueID value_ids[], uint32_t value_id_count, std::initializer_list<MeterValueID> ids)
14191439{
14201440 for (MeterValueID id : ids) {
0 commit comments