feat(agb02/control-box): auto-reconnect and white_button cycle
This commit is contained in:
parent
bcc156c5fc
commit
fecddb1f77
1 changed files with 31 additions and 24 deletions
|
@ -75,24 +75,6 @@ int main(const int argc, char const* const* const argv) {
|
||||||
// let the settings apply
|
// let the settings apply
|
||||||
std::this_thread::sleep_for(poll_period);
|
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<const sockaddr*>(&socket_addr),
|
|
||||||
sizeof(socket_addr));
|
|
||||||
if(con_ret < 0) {
|
|
||||||
std::cerr << "Failed to open tcp socket." << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// internal state and buffers ///
|
/// internal state and buffers ///
|
||||||
|
|
||||||
std::chrono::time_point now = std::chrono::system_clock::now();
|
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_pos = 0; //TODO: init from server
|
||||||
uint8_t decoder_realpos = read_decoder_realpos(line_reader);
|
uint8_t decoder_realpos = read_decoder_realpos(line_reader);
|
||||||
|
|
||||||
|
uint8_t white_state = 0;
|
||||||
bool white_pressed = false;
|
bool white_pressed = false;
|
||||||
bool black_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::chrono::time_point last_send = now;
|
||||||
std::string postData;
|
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<const sockaddr*>(&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(;;){
|
for(;;){
|
||||||
std::this_thread::sleep_for(poll_period);
|
std::this_thread::sleep_for(poll_period);
|
||||||
now = std::chrono::system_clock::now();
|
now = std::chrono::system_clock::now();
|
||||||
|
@ -153,8 +156,10 @@ int main(const int argc, char const* const* const argv) {
|
||||||
black_pressed = pressed;
|
black_pressed = pressed;
|
||||||
|
|
||||||
pressed = bool(line_reader.get_value(white_button));
|
pressed = bool(line_reader.get_value(white_button));
|
||||||
if(pressed ^ white_pressed)
|
if(pressed && !white_pressed){
|
||||||
has_changed = true;
|
has_changed = true;
|
||||||
|
white_state = (white_state + 1)%9;
|
||||||
|
}
|
||||||
white_pressed = pressed;
|
white_pressed = pressed;
|
||||||
|
|
||||||
/// decoder ///
|
/// decoder ///
|
||||||
|
@ -184,20 +189,22 @@ int main(const int argc, char const* const* const argv) {
|
||||||
"\"pan\": {},"
|
"\"pan\": {},"
|
||||||
"\"tilt\": {},"
|
"\"tilt\": {},"
|
||||||
"\"focus\": {},"
|
"\"focus\": {},"
|
||||||
"\"whiteButton\": {},"
|
"\"white_button\": {},"
|
||||||
"\"blackButton\": {}"
|
"\"black_button\": {}"
|
||||||
"}}\n",
|
"}}\n",
|
||||||
uint8_t(spot_pos.first),
|
uint8_t(spot_pos.first),
|
||||||
uint8_t(spot_pos.second),
|
uint8_t(spot_pos.second),
|
||||||
int(decoder_pos),
|
int(decoder_pos),
|
||||||
white_pressed,
|
white_state,
|
||||||
black_pressed
|
black_pressed
|
||||||
);
|
);
|
||||||
|
|
||||||
int wrote = write(socket_file_desc, postData.data(), postData.size());
|
int wrote = write(socket_file_desc, postData.data(), postData.size());
|
||||||
if(wrote < postData.size()){
|
if(wrote < postData.size()){
|
||||||
std::cerr << "Failed to send data, retrying in " << retry_timeout << "." << std::endl;
|
std::cerr << "Failed to send data, reconnecting..." << std::endl;
|
||||||
last_send = now + retry_timeout - server_ratelimit;
|
close(socket_file_desc);
|
||||||
|
std::this_thread::sleep_for(retry_timeout);
|
||||||
|
goto connection;
|
||||||
} else {
|
} else {
|
||||||
has_changed = false;
|
has_changed = false;
|
||||||
last_send = now;
|
last_send = now;
|
||||||
|
|
Loading…
Reference in a new issue