Skip to content

Commit cef4a7c

Browse files
IngmarSteinclaude
andcommitted
Add reboot command, richer client_info, and firmware version header
- Handle {"reboot": true} WebSocket command to reboot the Pi remotely - Send hostname and image_url in client_info on WebSocket connect - Send X-Firmware-Version header on HTTP image fetches Ported from ESP32 firmware feature set. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cf39722 commit cef4a7c

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

main.cc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <webp/decode.h>
22
#include <webp/demux.h>
33

4+
#include <unistd.h>
5+
46
#include <chrono>
57
#include <deque>
68
#include <fstream>
@@ -42,6 +44,15 @@ struct AppState {
4244

4345
static AppState *g_app_state = nullptr;
4446

47+
static std::string get_hostname() {
48+
char buf[256];
49+
if (gethostname(buf, sizeof(buf)) == 0) {
50+
buf[sizeof(buf) - 1] = '\0';
51+
return buf;
52+
}
53+
return "unknown";
54+
}
55+
4556
static std::string get_mac_address() {
4657
for (const char *iface_name : {"eth0", "wlan0"}) {
4758
std::ifstream iface(std::format("/sys/class/net/{}/address", iface_name));
@@ -385,7 +396,9 @@ int main(int argc, char *argv[]) {
385396
{{"firmware_version", FIRMWARE_VERSION},
386397
{"firmware_type", FIRMWARE_TYPE},
387398
{"protocol_version", PROTOCOL_VERSION},
388-
{"mac", get_mac_address()}}}};
399+
{"mac", get_mac_address()},
400+
{"hostname", get_hostname()},
401+
{"image_url", url}}}};
389402
Log(state, "Sending client info: " + client_info_msg.dump());
390403
ws_client->send(client_info_msg.dump());
391404
} else if (msg.type == ws::MessageType::Message) {
@@ -438,6 +451,13 @@ int main(int argc, char *argv[]) {
438451
state.queue_not_full.notify_all();
439452
immediate_requests++;
440453
}
454+
} else if (json_message.contains("reboot") &&
455+
json_message["reboot"].is_boolean()) {
456+
if (json_message["reboot"].get<bool>()) {
457+
Log(state, "Reboot requested via WebSocket");
458+
std::cerr << "Rebooting..." << std::endl;
459+
system("sudo reboot");
460+
}
441461
} else if (json_message.contains("status") &&
442462
json_message["status"].is_string() &&
443463
json_message.contains("message") &&
@@ -471,7 +491,8 @@ int main(int argc, char *argv[]) {
471491
}
472492
client->set_default_headers(
473493
{{"User-Agent", "Tronberry/1.0"},
474-
{"Accept", "image/webp, image/*;q=0.8, */*;q=0.5"}});
494+
{"Accept", "image/webp, image/*;q=0.8, */*;q=0.5"},
495+
{"X-Firmware-Version", FIRMWARE_VERSION}});
475496
client->set_connection_timeout(30);
476497
client->set_read_timeout(30);
477498
auto path = path_start != std::string::npos

0 commit comments

Comments
 (0)