feat(agb02): api implementation

This commit is contained in:
catvayor 2024-10-12 18:44:35 +02:00
parent 2d51b7265d
commit 012e5fb772
Signed by: lbailly
GPG key ID: CE3E645251AC63F3
4 changed files with 64 additions and 12 deletions

View file

@ -1,12 +1,18 @@
#include <curlpp/cURLpp.hpp> #include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <gpiod.hpp> #include <gpiod.hpp>
#include <iostream> #include <iostream>
#include <fstream>
#include <thread> #include <thread>
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
constexpr std::chrono::microseconds debounce = 40ms; constexpr std::chrono::microseconds debounce = 40ms;
constexpr std::chrono::microseconds poll_period = 5ms; constexpr std::chrono::microseconds poll_period = 5ms;
constexpr std::chrono::microseconds server_ratelimit = 50ms;
constexpr const char* url = "https://agb.hackens.org/api/control-box";
constexpr std::pair<double, double> joystick_movement(1.0, 1.0); constexpr std::pair<double, double> joystick_movement(1.0, 1.0);
@ -40,8 +46,10 @@ inline void clamp_decoder(uint8_t& decoder, int move){
} }
int main(const int argc, char const* const* const argv) { int main(const int argc, char const* const* const argv) {
if(argc < 2) if(argc < 3) {
std::cerr << "usage: agb gpiodevice tokenfile" << std::endl;
return 1; return 1;
}
/// init gpio chip /// /// init gpio chip ///
@ -69,8 +77,16 @@ int main(const int argc, char const* const* const argv) {
/// init server communication /// /// init server communication ///
cURLpp::initialize(); cURLpp::initialize();
std::string token;
{
std::ifstream tokenFile(argv[2]);
std::getline(tokenFile, token);
}
std::list<std::string> header;
header.push_back("Content-Type: application/json");
header.push_back("Authorization: Bearer " + token);
/// internal state /// /// internal state and buffers ///
gpiod::line::values joystick_read(4); gpiod::line::values joystick_read(4);
std::pair<double, double> spot_pos(0.0, 0.0); //TODO: init from server std::pair<double, double> spot_pos(0.0, 0.0); //TODO: init from server
@ -81,8 +97,9 @@ int main(const int argc, char const* const* const argv) {
bool white_pressed = false; bool white_pressed = false;
bool black_pressed = false; bool black_pressed = false;
bool has_changed = false;
std::chrono::time_point last_send = std::chrono::system_clock::now();
std::string postData;
for(;;){ for(;;){
std::this_thread::sleep_for(poll_period); std::this_thread::sleep_for(poll_period);
@ -97,32 +114,30 @@ int main(const int argc, char const* const* const argv) {
if (bool(joystick_read[0]) || bool(joystick_read[1]) if (bool(joystick_read[0]) || bool(joystick_read[1])
|| bool(joystick_read[2]) || bool(joystick_read[3])) || bool(joystick_read[2]) || bool(joystick_read[3]))
; //TODO: send to server has_changed = true;
/// Buttons /// /// Buttons ///
bool pressed = bool(line_reader.get_value(black_button)); bool pressed = bool(line_reader.get_value(black_button));
if(pressed ^ black_pressed) if(pressed ^ black_pressed)
; //TODO: send to server has_changed = true;
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)
; //TODO: send to server has_changed = true;
white_pressed = pressed; white_pressed = pressed;
/// decoder /// /// decoder ///
uint8_t new_realpos = read_decoder_realpos(line_reader); uint8_t new_realpos = read_decoder_realpos(line_reader);
uint8_t seen_travel = std::abs(int(new_realpos) - int(decoder_realpos)); uint8_t seen_travel = std::abs(int(new_realpos) - int(decoder_realpos));
//TODO: check CW and CCW // CCW
// CW
if(seen_travel < 50 && new_realpos < decoder_realpos) if(seen_travel < 50 && new_realpos < decoder_realpos)
clamp_decoder(decoder_pos, -seen_travel); clamp_decoder(decoder_pos, -seen_travel);
if(seen_travel >= 50 && new_realpos > decoder_realpos) if(seen_travel >= 50 && new_realpos > decoder_realpos)
clamp_decoder(decoder_pos, seen_travel - 128); clamp_decoder(decoder_pos, seen_travel - 128);
// CCW // CW
if(seen_travel < 50 && new_realpos > decoder_realpos) if(seen_travel < 50 && new_realpos > decoder_realpos)
clamp_decoder(decoder_pos, seen_travel); clamp_decoder(decoder_pos, seen_travel);
if(seen_travel >= 50 && new_realpos < decoder_realpos) if(seen_travel >= 50 && new_realpos < decoder_realpos)
@ -130,6 +145,38 @@ int main(const int argc, char const* const* const argv) {
decoder_realpos = new_realpos; decoder_realpos = new_realpos;
if(seen_travel) if(seen_travel)
; //TODO: send to server has_changed = true;
/// server notification
std::chrono::time_point now = std::chrono::system_clock::now();
if(has_changed && (now - last_send > server_ratelimit)){
curlpp::Easy request;
request.setOpt(curlpp::options::Url(url));
request.setOpt(curlpp::options::HttpHeader(header));
postData.clear();
std::format_to(std::back_inserter(postData), "{{"
"\"pan\": {},"
"\"tilt\": {},"
"\"focus\": {},"
"\"whiteButton\": {},"
"\"blackButton\": {}"
"}}",
spot_pos.first,
spot_pos.second,
int(decoder_pos),
white_pressed,
black_pressed
);
request.setOpt(curlpp::options::PostFields(postData));
request.setOpt(curlpp::options::PostFieldSize(postData.size()));
//request.perform();
// std::cout << std::chrono::system_clock::now() - now << std::endl;
std::cout << postData << std::endl;
has_changed = false;
last_send = now;
}
} }
} }

View file

@ -8,4 +8,8 @@
file = ./wg.age; file = ./wg.age;
owner = "systemd-network"; owner = "systemd-network";
}; };
age.secrets."token" = {
file = ./token.age;
# owner = "systemd-network";
};
} }

View file

@ -5,4 +5,5 @@ let
(builtins.readFile (../../../pubkeys + "/${user}.keys"))); (builtins.readFile (../../../pubkeys + "/${user}.keys")));
in { in {
"wg.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02"); "wg.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02");
"token.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02");
} }

Binary file not shown.