aqhome: adapted server aqhome-mqttlog to events2 api.
This commit is contained in:
@@ -10,10 +10,7 @@
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "./init.h"
|
||||
#include "./fini.h"
|
||||
#include "./loop.h"
|
||||
#include "./loop_mqtt.h"
|
||||
#include "./server.h"
|
||||
|
||||
#include "aqhome/aqhome.h"
|
||||
|
||||
@@ -46,8 +43,9 @@
|
||||
#define I18N(msg) msg
|
||||
#define I18S(msg) msg
|
||||
|
||||
#define AQHOME_MQTTLOG_PING_INTERVAL 120
|
||||
#define AQHOME_MQTTLOG_SAVE_INTERVAL 60
|
||||
#define CONNCHECK_INTERVAL_IN_SECS 10
|
||||
#define PING_INTERVAL_IN_SECS 120
|
||||
#define SAVE_INTERVAL_IN_SECS 60
|
||||
|
||||
#define FULL_DEBUG
|
||||
|
||||
@@ -58,7 +56,8 @@
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static void _serve(AQHOME_MQTT *aqh);
|
||||
static void _runService(AQH_OBJECT *aqh, AQH_EVENT_LOOP *eventLoop);
|
||||
static int _diffInSeconds(time_t t1, time_t t0);
|
||||
|
||||
#ifdef HAVE_SIGNAL_H
|
||||
static int _setSignalHandlers(void);
|
||||
@@ -85,11 +84,10 @@ static int stopService=0;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
AQHOME_MQTT *aqh;
|
||||
GWEN_DB_NODE *dbArgs;
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_OBJECT *aqh;
|
||||
int rv;
|
||||
GWEN_GUI *gui;
|
||||
const char *s;
|
||||
|
||||
rv=GWEN_Init();
|
||||
if (rv) {
|
||||
@@ -100,14 +98,25 @@ int main(int argc, char **argv)
|
||||
GWEN_Logger_Open(0, "aqhome-mqttlog", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User);
|
||||
GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning);
|
||||
|
||||
rv=_setSignalHandlers();
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv=AQH_Init();
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
return 2;
|
||||
}
|
||||
|
||||
aqh=AqHomeMqtt_new();
|
||||
rv=AqHomeMqtt_Init(aqh, argc, argv);
|
||||
gui=GWEN_Gui_CGui_new();
|
||||
GWEN_Gui_SetGui(gui);
|
||||
|
||||
eventLoop=AQH_EventLoop_new();
|
||||
|
||||
aqh=AQH_MqttLogServer_new(eventLoop);
|
||||
rv=AQH_MqttLogServer_Init(aqh, argc, argv);
|
||||
if (rv<0) {
|
||||
if (rv==GWEN_ERROR_CLOSE)
|
||||
return 1;
|
||||
@@ -115,18 +124,11 @@ int main(int argc, char **argv)
|
||||
return 2;
|
||||
}
|
||||
|
||||
dbArgs=AqHomeMqtt_GetDbArgs(aqh);
|
||||
_runService(aqh, eventLoop);
|
||||
|
||||
gui=GWEN_Gui_CGui_new();
|
||||
s=GWEN_DB_GetCharValue(dbArgs, "charset", 0, NULL);
|
||||
if (s && *s)
|
||||
GWEN_Gui_SetCharSet(gui, s);
|
||||
GWEN_Gui_SetGui(gui);
|
||||
|
||||
_serve(aqh);
|
||||
|
||||
AqHomeMqtt_Fini(aqh);
|
||||
AqHomeMqtt_free(aqh);
|
||||
AQH_MqttLogServer_Fini(aqh);
|
||||
AQH_Object_free(aqh);
|
||||
|
||||
GWEN_Gui_SetGui(NULL);
|
||||
GWEN_Gui_free(gui);
|
||||
@@ -136,77 +138,65 @@ int main(int argc, char **argv)
|
||||
|
||||
|
||||
|
||||
void _serve(AQHOME_MQTT *aqh)
|
||||
void _runService(AQH_OBJECT *aqh, AQH_EVENT_LOOP *eventLoop)
|
||||
{
|
||||
int rv;
|
||||
time_t timeStart;
|
||||
int timeout;
|
||||
time_t startTime;
|
||||
time_t lastPingSendTime;
|
||||
time_t lastSaveTime;
|
||||
GWEN_DB_NODE *dbArgs;
|
||||
time_t timeLastConnCheck;
|
||||
time_t timeLastSave;
|
||||
time_t timeLastPingSend;
|
||||
int rv;
|
||||
|
||||
startTime=time(NULL);
|
||||
lastSaveTime=time(NULL);
|
||||
|
||||
dbArgs=AqHomeMqtt_GetDbArgs(aqh);
|
||||
|
||||
rv=_setSignalHandlers();
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error setting signal handlers (%d)", rv);
|
||||
return;
|
||||
}
|
||||
|
||||
timeout=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 0);
|
||||
|
||||
lastPingSendTime=time(NULL);
|
||||
timeout=AQH_MqttLogServer_GetTimeout(aqh);
|
||||
timeStart=time(NULL);
|
||||
timeLastConnCheck=time(NULL);
|
||||
timeLastSave=time(NULL);
|
||||
timeLastPingSend=time(NULL);
|
||||
|
||||
while(!stopService) {
|
||||
DBG_DEBUG(NULL, "Next loop");
|
||||
AqHomeMqttLog_Loop(aqh, 2000);
|
||||
time_t now;
|
||||
|
||||
if (timeout) {
|
||||
time_t now;
|
||||
AQH_EventLoop_Run(eventLoop, 2000);
|
||||
AQH_MqttLogServer_HandleMqttMsgs(aqh);
|
||||
AQH_MqttLogServer_HandleBrokerMsgs(aqh);
|
||||
|
||||
now=time(NULL);
|
||||
if ((now-startTime)>timeout) {
|
||||
DBG_ERROR(NULL, "Timeout, stopping service");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (1){
|
||||
time_t now;
|
||||
now=time(NULL);
|
||||
|
||||
now=time(NULL);
|
||||
if (now-lastPingSendTime>AQHOME_MQTTLOG_PING_INTERVAL) {
|
||||
rv=AqHomeMqttLog_SendPing(aqh);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "Error sending PING");
|
||||
}
|
||||
lastPingSendTime=time(NULL);
|
||||
}
|
||||
if (_diffInSeconds(now, timeLastConnCheck)>CONNCHECK_INTERVAL_IN_SECS) {
|
||||
DBG_ERROR(NULL, "Check connections");
|
||||
AQH_MqttLogServer_CheckBrokerConnection(aqh);
|
||||
AQH_MqttLogServer_CheckMqttConnection(aqh);
|
||||
timeLastConnCheck=now;
|
||||
}
|
||||
|
||||
if (1){
|
||||
time_t now;
|
||||
|
||||
now=time(NULL);
|
||||
if (now-lastSaveTime>AQHOME_MQTTLOG_SAVE_INTERVAL) {
|
||||
DBG_ERROR(NULL, "Writing device files");
|
||||
rv=AqHomeMqtt_SaveRuntimeDeviceFiles(aqh);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "Error writing runtime data");
|
||||
}
|
||||
lastSaveTime=time(NULL);
|
||||
if (_diffInSeconds(now, timeLastPingSend)>PING_INTERVAL_IN_SECS) {
|
||||
rv=AQH_MqttLogServer_SendPing(aqh);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "Error sending PING");
|
||||
}
|
||||
timeLastPingSend=time(NULL);
|
||||
}
|
||||
|
||||
if (_diffInSeconds(now, timeLastSave)>SAVE_INTERVAL_IN_SECS) {
|
||||
DBG_ERROR(NULL, "Writing device files");
|
||||
rv=AQH_MqttLogServer_SaveRuntimeDeviceFiles(aqh);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "Error writing device file");
|
||||
}
|
||||
timeLastSave=time(NULL);
|
||||
}
|
||||
|
||||
if (timeout && (_diffInSeconds(now, timeStart)>timeout)) {
|
||||
DBG_INFO(NULL, "Timeout");
|
||||
break;
|
||||
}
|
||||
} /* while */
|
||||
|
||||
rv=AqHomeMqtt_SaveRuntimeDeviceFiles(aqh);
|
||||
|
||||
rv=AQH_MqttLogServer_SaveRuntimeDeviceFiles(aqh);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "Error writing runtime data");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -277,3 +267,8 @@ void _signalHandler(int s)
|
||||
|
||||
|
||||
|
||||
int _diffInSeconds(time_t t1, time_t t0)
|
||||
{
|
||||
return t1-t0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user