Make watcher tollerant of starting after server it connectes to
This commit is contained in:
parent
4ea099db1a
commit
4b0b5847a7
1 changed files with 53 additions and 8 deletions
61
main.cpp
61
main.cpp
|
|
@ -29,18 +29,18 @@ struct PwlPriv
|
|||
|
||||
struct Config config;
|
||||
|
||||
TCPSocket* socket;
|
||||
TCPSocket* socket = nullptr;
|
||||
|
||||
spa_source* timer_source;
|
||||
struct timespec timout_timespan;
|
||||
};
|
||||
|
||||
static void send_sensor(TCPSocket* socket, uint8_t id, bool state)
|
||||
static bool send_sensor(TCPSocket* socket, uint8_t id, bool state)
|
||||
{
|
||||
static int8_t lastState = -1;
|
||||
|
||||
if(state == lastState)
|
||||
return;
|
||||
return true;
|
||||
|
||||
lastState = state;
|
||||
|
||||
|
|
@ -50,13 +50,48 @@ static void send_sensor(TCPSocket* socket, uint8_t id, bool state)
|
|||
json.append(" }] }\n");
|
||||
std::string sendbuf(MSG_STRING + std::to_string(json.length()) + "\n" + json);
|
||||
Log(Log::DEBUG)<<"Sending: \n"<<sendbuf;
|
||||
socket->send(sendbuf.c_str(), sendbuf.size());
|
||||
|
||||
try
|
||||
{
|
||||
socket->send(sendbuf.c_str(), sendbuf.size());
|
||||
}
|
||||
catch(SocketException &e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static TCPSocket* check_socket(PwlPriv *priv)
|
||||
{
|
||||
if(priv->socket)
|
||||
return priv->socket;
|
||||
try
|
||||
{
|
||||
priv->socket = new TCPSocket(priv->config.host, priv->config.port);
|
||||
}
|
||||
catch(SocketException &e)
|
||||
{
|
||||
Log(Log::WARN)<<e.what();
|
||||
delete priv->socket;
|
||||
priv->socket = nullptr;
|
||||
}
|
||||
return priv->socket;
|
||||
}
|
||||
|
||||
static void off_timeout(void *data, uint64_t expirations)
|
||||
{
|
||||
PwlPriv *priv = reinterpret_cast<PwlPriv*>(data);
|
||||
send_sensor(priv->socket, priv->config.id, false);
|
||||
priv->socket = check_socket(priv);
|
||||
if(priv->socket)
|
||||
{
|
||||
bool ret = send_sensor(priv->socket, priv->config.id, false);
|
||||
if(!ret)
|
||||
{
|
||||
delete priv->socket;
|
||||
priv->socket = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void state_changed(PwlPriv *priv, bool state)
|
||||
|
|
@ -70,7 +105,16 @@ static void state_changed(PwlPriv *priv, bool state)
|
|||
|
||||
if(state)
|
||||
{
|
||||
send_sensor(priv->socket, priv->config.id, true);
|
||||
priv->socket = check_socket(priv);
|
||||
if(priv->socket)
|
||||
{
|
||||
bool ret = send_sensor(priv->socket, priv->config.id, false);
|
||||
if(!ret)
|
||||
{
|
||||
delete priv->socket;
|
||||
priv->socket = nullptr;
|
||||
}
|
||||
}
|
||||
pw_loop_update_timer(pw_main_loop_get_loop(priv->loop), priv->timer_source, NULL, NULL, false);
|
||||
}
|
||||
else
|
||||
|
|
@ -261,8 +305,9 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
catch(SocketException &e)
|
||||
{
|
||||
Log(Log::ERROR)<<e.what();
|
||||
return 1;
|
||||
Log(Log::WARN)<<e.what();
|
||||
delete priv.socket;
|
||||
priv.socket = nullptr;
|
||||
}
|
||||
|
||||
priv.loop = pw_main_loop_new(NULL);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue