2023-01-09 17:03:52 +01:00
|
|
|
#include "ServerAPI.h"
|
|
|
|
|
#include "configuration.h"
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
template <typename T>
|
2023-01-09 17:03:52 +01:00
|
|
|
ServerAPI<T>::ServerAPI(T &_client) : StreamAPI(&client), concurrency::OSThread("ServerAPI"), client(_client)
|
|
|
|
|
{
|
|
|
|
|
LOG_INFO("Incoming wifi connection\n");
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
template <typename T> ServerAPI<T>::~ServerAPI()
|
2023-01-09 17:03:52 +01:00
|
|
|
{
|
|
|
|
|
client.stop();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
template <typename T> void ServerAPI<T>::close()
|
2023-01-09 17:03:52 +01:00
|
|
|
{
|
|
|
|
|
client.stop(); // drop tcp connection
|
|
|
|
|
StreamAPI::close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check the current underlying physical link to see if the client is currently connected
|
2023-01-21 14:34:29 +01:00
|
|
|
template <typename T> bool ServerAPI<T>::checkIsConnected()
|
2023-01-09 17:03:52 +01:00
|
|
|
{
|
|
|
|
|
return client.connected();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
template <class T> int32_t ServerAPI<T>::runOnce()
|
2023-01-09 17:03:52 +01:00
|
|
|
{
|
|
|
|
|
if (client.connected()) {
|
|
|
|
|
return StreamAPI::runOncePart();
|
|
|
|
|
} else {
|
|
|
|
|
LOG_INFO("Client dropped connection, suspending API service\n");
|
|
|
|
|
enabled = false; // we no longer need to run
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
template <class T, class U> APIServerPort<T, U>::APIServerPort(int port) : U(port), concurrency::OSThread("ApiServer") {}
|
2023-01-09 17:03:52 +01:00
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
template <class T, class U> void APIServerPort<T, U>::init()
|
2023-01-09 17:03:52 +01:00
|
|
|
{
|
|
|
|
|
U::begin();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
template <class T, class U> int32_t APIServerPort<T, U>::runOnce()
|
2023-01-09 17:03:52 +01:00
|
|
|
{
|
|
|
|
|
auto client = U::available();
|
|
|
|
|
if (client) {
|
|
|
|
|
// Close any previous connection (see FIXME in header file)
|
|
|
|
|
if (openAPI) {
|
|
|
|
|
LOG_INFO("Force closing previous TCP connection\n");
|
|
|
|
|
delete openAPI;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openAPI = new T(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 100; // only check occasionally for incoming connections
|
|
|
|
|
}
|