From fecddb1f7720d40cacdcfd7b7e91d4f9c0289184 Mon Sep 17 00:00:00 2001 From: catvayor Date: Tue, 15 Oct 2024 16:53:44 +0200 Subject: [PATCH] feat(agb02/control-box): auto-reconnect and white_button cycle --- machines/agb02/agb/agb.cpp | 55 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/machines/agb02/agb/agb.cpp b/machines/agb02/agb/agb.cpp index 022d132..107b20b 100644 --- a/machines/agb02/agb/agb.cpp +++ b/machines/agb02/agb/agb.cpp @@ -75,24 +75,6 @@ int main(const int argc, char const* const* const argv) { // let the settings apply std::this_thread::sleep_for(poll_period); - /// init server communication /// - - int socket_file_desc = socket(AF_INET, SOCK_STREAM, 0); - { - sockaddr_in socket_addr = { - .sin_family = AF_INET, - .sin_port = htons(1235), - .sin_addr = { .s_addr = inet_addr("10.10.10.1") } - }; - int con_ret = connect(socket_file_desc, - reinterpret_cast(&socket_addr), - sizeof(socket_addr)); - if(con_ret < 0) { - std::cerr << "Failed to open tcp socket." << std::endl; - return 1; - } - } - /// internal state and buffers /// std::chrono::time_point now = std::chrono::system_clock::now(); @@ -119,6 +101,7 @@ int main(const int argc, char const* const* const argv) { uint8_t decoder_pos = 0; //TODO: init from server uint8_t decoder_realpos = read_decoder_realpos(line_reader); + uint8_t white_state = 0; bool white_pressed = false; bool black_pressed = false; @@ -126,6 +109,26 @@ int main(const int argc, char const* const* const argv) { std::chrono::time_point last_send = now; std::string postData; + /// init server communication /// + + int socket_file_desc; +connection: + socket_file_desc = socket(AF_INET, SOCK_STREAM, 0); + { + sockaddr_in socket_addr = { + .sin_family = AF_INET, + .sin_port = htons(1235), + .sin_addr = { .s_addr = inet_addr("10.10.10.1") } + }; + while (connect(socket_file_desc, + reinterpret_cast(&socket_addr), + sizeof(socket_addr)) < 0) { + std::cerr << "Failed to open tcp socket, retrying..." << std::endl; + std::this_thread::sleep_for(retry_timeout); + } + std::cout << "Connected." << std::endl; + } + for(;;){ std::this_thread::sleep_for(poll_period); now = std::chrono::system_clock::now(); @@ -153,8 +156,10 @@ int main(const int argc, char const* const* const argv) { black_pressed = pressed; pressed = bool(line_reader.get_value(white_button)); - if(pressed ^ white_pressed) + if(pressed && !white_pressed){ has_changed = true; + white_state = (white_state + 1)%9; + } white_pressed = pressed; /// decoder /// @@ -184,20 +189,22 @@ int main(const int argc, char const* const* const argv) { "\"pan\": {}," "\"tilt\": {}," "\"focus\": {}," - "\"whiteButton\": {}," - "\"blackButton\": {}" + "\"white_button\": {}," + "\"black_button\": {}" "}}\n", uint8_t(spot_pos.first), uint8_t(spot_pos.second), int(decoder_pos), - white_pressed, + white_state, black_pressed ); int wrote = write(socket_file_desc, postData.data(), postData.size()); if(wrote < postData.size()){ - std::cerr << "Failed to send data, retrying in " << retry_timeout << "." << std::endl; - last_send = now + retry_timeout - server_ratelimit; + std::cerr << "Failed to send data, reconnecting..." << std::endl; + close(socket_file_desc); + std::this_thread::sleep_for(retry_timeout); + goto connection; } else { has_changed = false; last_send = now;