ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
online_image.h
Go to the documentation of this file.
1#pragma once
2
3#include "download_buffer.h"
10
11namespace esphome::online_image {
12
13using t_http_codes = enum {
14 HTTP_CODE_OK = 200,
15 HTTP_CODE_NOT_MODIFIED = 304,
16 HTTP_CODE_NOT_FOUND = 404,
17};
18
21static constexpr size_t READ_CHUNK = 1460;
22
28class OnlineImage final : public PollingComponent,
30 public Parented<esphome::http_request::HttpRequestComponent> {
31 public:
45 OnlineImage(const std::string &url, int width, int height, runtime_image::ImageFormat format, image::ImageType type,
46 image::Transparency transparency, image::Image *placeholder, uint32_t buffer_size,
47 bool is_big_endian = false);
48
49 void update() override;
51 void update_if(bool update) {
52 if (update)
53 this->update();
54 }
55 void loop() override;
56
58 void set_url(const std::string &url) {
59 if (this->validate_url_(url)) {
60 this->url_ = url;
61 }
62 this->etag_ = "";
63 this->last_modified_ = "";
64 }
65
67 template<typename V> void add_request_header(const std::string &header, V value) {
68 this->request_headers_.push_back(std::pair<std::string, TemplatableValue<std::string>>(header, value));
69 }
70
75 void release();
76
78 template<typename F> void add_on_download_started_callback(F &&callback) {
79 this->download_started_callback_.add(std::forward<F>(callback));
80 }
81 template<typename F> void add_on_finished_callback(F &&callback) {
82 this->download_finished_callback_.add(std::forward<F>(callback));
83 }
85 template<typename F> void add_on_progress_callback(F &&callback) {
86 this->download_progress_callback_.add(std::forward<F>(callback));
87 }
88 template<typename F> void add_on_error_callback(F &&callback) {
89 this->download_error_callback_.add(std::forward<F>(callback));
90 }
91
92 protected:
93 bool validate_url_(const std::string &url);
94 void end_connection_();
95 void finish_download_();
97 void loop_stream_();
98 void fail_download_();
100 void report_progress_(bool force);
101
106
107 std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
116
117 std::string url_;
118
119 std::vector<std::pair<std::string, TemplatableValue<std::string>>> request_headers_;
120
124 std::string etag_;
128 std::string last_modified_;
129
133 bool streaming_{false}; // The decoder reads the download itself, as it arrives
134 bool stream_started_{false}; // The decoder has read the headers
135};
136
137} // namespace esphome::online_image
Helper class to easily give an object a parent of type T.
Definition helpers.h:2073
This class simplifies creating components that periodically check a state.
Definition component.h:519
Primary TemplatableValue: stores either a constant value or a function pointer.
Definition automation.h:168
Buffer for managing downloaded data.
Lets a decoder read a download as it arrives, from the download buffer.
Download an image from a given URL, and decode it using the specified decoder.
std::string etag_
The value of the ETag HTTP header provided in the last response.
OnlineImage(const std::string &url, int width, int height, runtime_image::ImageFormat format, image::ImageType type, image::Transparency transparency, image::Image *placeholder, uint32_t buffer_size, bool is_big_endian=false)
Construct a new OnlineImage object.
LazyCallbackManager< void()> download_started_callback_
void loop_stream_()
loop() for a JPEG that is decoded as it downloads
bool validate_url_(const std::string &url)
LazyCallbackManager< void(float, size_t)> download_progress_callback_
void add_on_finished_callback(F &&callback)
void add_on_download_started_callback(F &&callback)
Called when the server has accepted the request and the image starts to download.
CallbackManager< void(bool)> download_finished_callback_
std::vector< std::pair< std::string, TemplatableValue< std::string > > > request_headers_
void add_request_header(const std::string &header, V value)
Add the request header.
void set_url(const std::string &url)
Set the URL to download the image from.
std::shared_ptr< http_request::HttpContainer > downloader_
CallbackManager< void()> download_error_callback_
void update_if(bool update)
Download now when update is true (the update flag of online_image.set_url).
size_t download_buffer_initial_size_
This is the initial size of the download buffer, not the current size.
void report_progress_(bool force)
Reports download progress, at most every PROGRESS_INTERVAL_MS unless force is set.
std::string last_modified_
The value of the Last-Modified HTTP header provided in the last response.
void add_on_progress_callback(F &&callback)
Called with the percentage downloaded (NAN when the server did not send the size) and the bytes read ...
void add_on_error_callback(F &&callback)
void release()
Release the buffer storing the image.
A dynamic image that can be loaded and decoded at runtime.
uint8_t type
const char * format
enum { HTTP_CODE_OK=200, HTTP_CODE_NOT_MODIFIED=304, HTTP_CODE_NOT_FOUND=404, } t_http_codes
ImageFormat
Image format types that can be decoded dynamically.
static void uint32_t