mqtt module now works.
This commit is contained in:
@@ -54,8 +54,6 @@
|
||||
loop_mqtt.h
|
||||
aqhome_mqtt.h
|
||||
aqhome_mqtt_p.h
|
||||
mqtt.h
|
||||
messages.h
|
||||
xmlread.h
|
||||
</headers>
|
||||
|
||||
@@ -68,8 +66,6 @@
|
||||
loop_ipc.c
|
||||
loop_mqtt.c
|
||||
main.c
|
||||
mqtt.c
|
||||
messages.c
|
||||
xmlread.c
|
||||
</sources>
|
||||
|
||||
|
||||
@@ -12,10 +12,10 @@
|
||||
|
||||
|
||||
#include "./init.h"
|
||||
#include "./mqtt.h"
|
||||
#include "./aqhome_mqtt_p.h"
|
||||
#include "./xmlread.h"
|
||||
|
||||
#include "aqhome/aqhome.h"
|
||||
#include "aqhome/ipc/endpoint_ipc.h"
|
||||
#include "aqhome/ipc/endpoint_ipcclient.h"
|
||||
#include "aqhome/mqtt/endpoint_mqttc.h"
|
||||
@@ -72,6 +72,7 @@ int AqHomeMqtt_Init(AQHOME_MQTT *aqh, int argc, char **argv)
|
||||
DBG_ERROR(NULL, "Error reading args (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
AQH_MergeConfigFileIntoConfig(dbArgs, "ConfigFile");
|
||||
aqh->dbArgs=dbArgs;
|
||||
|
||||
aqh->timeout=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 0);
|
||||
@@ -100,6 +101,8 @@ int AqHomeMqtt_Init(AQHOME_MQTT *aqh, int argc, char **argv)
|
||||
return rv;
|
||||
}
|
||||
|
||||
AqHomeMqtt_ReloadDeviceFiles(aqh);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -109,7 +112,7 @@ void AqHomeMqtt_ReloadDeviceFiles(AQHOME_MQTT *aqh)
|
||||
{
|
||||
AQHMQTT_DEVICE_LIST *deviceList;
|
||||
|
||||
deviceList=AqHomeMqttLog_ReadSysconfDeviceFiles(aqh);
|
||||
deviceList=AqHomeMqttLog_ReadDataDeviceFiles(aqh);
|
||||
if (deviceList)
|
||||
AqHomeMqtt_SetAvailableDeviceList(aqh, deviceList);
|
||||
}
|
||||
@@ -159,7 +162,13 @@ int _setupBroker(AQHOME_MQTT *aqh, GWEN_DB_NODE *dbArgs)
|
||||
const char *brokerClientId;
|
||||
|
||||
brokerAddress=GWEN_DB_GetCharValue(dbArgs, "brokerAddress", 0, NULL);
|
||||
brokerPort=GWEN_DB_GetIntValue(dbArgs, "brokerPort", 0, AQHOME_MQTT_DEFAULT_BROKER_PORT);
|
||||
if (!(brokerAddress && *brokerAddress))
|
||||
brokerAddress=GWEN_DB_GetCharValue(dbArgs, "ConfigFile/brokerAddress", 0, "127.0.0.1");
|
||||
|
||||
brokerPort=GWEN_DB_GetIntValue(dbArgs, "brokerPort", 0, -1);
|
||||
if (brokerPort<0)
|
||||
brokerPort=GWEN_DB_GetIntValue(dbArgs, "ConfigFile/brokerPort", 0, AQHOME_MQTT_DEFAULT_BROKER_PORT);
|
||||
|
||||
brokerClientId=GWEN_DB_GetCharValue(dbArgs, "brokerClientId", 0, AQHOME_MQTT_DEFAULT_BROKER_CLIENTID);
|
||||
|
||||
if (brokerAddress && *brokerAddress && brokerPort) {
|
||||
@@ -189,22 +198,45 @@ int _setupBroker(AQHOME_MQTT *aqh, GWEN_DB_NODE *dbArgs)
|
||||
|
||||
int _setupMqtt(AQHOME_MQTT *aqh, GWEN_DB_NODE *dbArgs)
|
||||
{
|
||||
GWEN_MSG_ENDPOINT *ep;
|
||||
const char *mqttAddress;
|
||||
int mqttPort;
|
||||
const char *mqttClientId;
|
||||
int mqttKeepAlive;
|
||||
|
||||
ep=AqHomeMqttLog_CreateMqttEndpoint(dbArgs);
|
||||
if (ep==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint for MQTT");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
mqttAddress=GWEN_DB_GetCharValue(dbArgs, "mqttAddress", 0, NULL);
|
||||
if (!(mqttAddress && *mqttAddress))
|
||||
mqttAddress=GWEN_DB_GetCharValue(dbArgs, "ConfigFile/mqttAddr", 0, "127.0.0.1");
|
||||
|
||||
mqttPort=GWEN_DB_GetIntValue(dbArgs, "mqttPort", 0, 1883);
|
||||
if (mqttPort<0)
|
||||
mqttPort=GWEN_DB_GetIntValue(dbArgs, "ConfigFile/mqttPort", 0, 1883);
|
||||
|
||||
mqttClientId=GWEN_DB_GetCharValue(dbArgs, "mqttClientId", 0, "aqhome-mqttlog");
|
||||
mqttKeepAlive=GWEN_DB_GetIntValue(dbArgs, "mqttKeepAlive", 0, 600);
|
||||
|
||||
if (mqttAddress && *mqttAddress && mqttPort) {
|
||||
GWEN_MSG_ENDPOINT *epMqtt;
|
||||
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Connecting to %s (port %d)", mqttAddress, mqttPort);
|
||||
epMqtt=AQH_MqttClientEndpoint_new(mqttClientId, mqttAddress, mqttPort, NULL, 0);
|
||||
if (epMqtt==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint TCP");
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
AQH_MqttClientEndpoint_SetKeepAliveTime(epMqtt, mqttKeepAlive);
|
||||
GWEN_MsgEndpoint_AddFlags(epMqtt, AQH_ENDPOINT2_MQTTCLIENT_FLAGS_SUBSCRIBEALL);
|
||||
|
||||
GWEN_MsgEndpoint_Tree2_AddChild(aqh->rootEndpoint, epMqtt);
|
||||
aqh->mqttEndpoint=epMqtt;
|
||||
}
|
||||
|
||||
GWEN_MsgEndpoint_Tree2_AddChild(aqh->rootEndpoint, ep);
|
||||
aqh->mqttEndpoint=ep;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int _readArgs(int argc, char **argv, GWEN_DB_NODE *dbArgs)
|
||||
{
|
||||
int rv;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
|
||||
int AqHomeMqtt_Init(AQHOME_MQTT *aqh, int argc, char **argv);
|
||||
void AqHomeMqtt_ReloadDeviceFiles(AQHOME_MQTT *aqh);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include <gwenhywfar/db.h>
|
||||
|
||||
|
||||
#define FULL_DEBUG
|
||||
//#define FULL_DEBUG
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
@@ -90,6 +90,7 @@ int AqHomeMqttLog_SendPing(AQHOME_MQTT *aqh)
|
||||
void _handleMqttMsg(AQHOME_MQTT *aqh, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(AQH_MQTTMSG_MSGTYPE_PUBLISH & 0xf0)) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "PUBLISH message received");
|
||||
#ifdef FULL_DEBUG
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ 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);
|
||||
/*GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Info);*/
|
||||
//GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Info);
|
||||
|
||||
rv=AQH_Init();
|
||||
if (rv<0) {
|
||||
|
||||
@@ -1,274 +0,0 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "./messages.h"
|
||||
|
||||
#include "aqhome/mqtt/msg_mqtt_publish.h"
|
||||
|
||||
#include <gwenhywfar/json_read.h>
|
||||
#include <gwenhywfar/db.h>
|
||||
#include <gwenhywfar/directory.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
static const ITEM *_getItemForTopic(const ITEM_LIST *itemList, const char *topic);
|
||||
|
||||
void _handlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg);
|
||||
void _handlePublish(const char *baseFolder, const ITEM_LIST *itemList, const char *topic, const char *value);
|
||||
void _handleRawMsgForItem(const char *baseFolder, const ITEM *item, const char *value);
|
||||
void _handleJsonMsgForItem(const char *baseFolder, const ITEM *item, const char *value);
|
||||
void _handleJsonItemVar(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, GWEN_JSON_ELEM *jeRoot);
|
||||
void _writeValueAccordingToItem(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, const char *value);
|
||||
void _writeToFile(const char *filename, const char *txt);
|
||||
|
||||
|
||||
|
||||
|
||||
ITEM_LIST *AqHomeMqttLog_ReadItems(GWEN_DB_NODE *dbArgs)
|
||||
{
|
||||
const char *itemFile;
|
||||
|
||||
itemFile=GWEN_DB_GetCharValue(dbArgs, "itemfile", 0, NULL);
|
||||
if (itemFile && *itemFile) {
|
||||
ITEM_LIST *itemList;
|
||||
GWEN_DB_NODE *dbItemList;
|
||||
GWEN_DB_NODE *dbItem;
|
||||
int rv;
|
||||
|
||||
dbItemList=GWEN_DB_Group_new("items");
|
||||
rv=GWEN_DB_ReadFile(dbItemList, itemFile,
|
||||
GWEN_DB_FLAGS_DEFAULT |
|
||||
GWEN_PATH_FLAGS_CREATE_GROUP |
|
||||
GWEN_DB_FLAGS_ALLOW_EMPTY_STREAM);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error reading item file \"%s\" (%d)", itemFile, rv);
|
||||
GWEN_DB_Group_free(dbItemList);
|
||||
return NULL;
|
||||
}
|
||||
itemList=Item_List_new();
|
||||
dbItem=GWEN_DB_FindFirstGroup(dbItemList, "item");
|
||||
while(dbItem) {
|
||||
ITEM *item;
|
||||
|
||||
item=Item_fromDb(dbItem);
|
||||
Item_List_Add(item, itemList);
|
||||
dbItem=GWEN_DB_FindNextGroup(dbItem, "item");
|
||||
}
|
||||
GWEN_DB_Group_free(dbItemList);
|
||||
|
||||
if (Item_List_GetCount(itemList)==0) {
|
||||
DBG_INFO(NULL, "No items in file");
|
||||
Item_List_free(itemList);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return itemList;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AqHomeMqttLog_HandlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg)
|
||||
{
|
||||
char *topic;
|
||||
char *value;
|
||||
|
||||
topic=AQH_PublishMqttMsg_ExtractTopic(msg);
|
||||
value=AQH_PublishMqttMsg_ExtractValue(msg);
|
||||
|
||||
if (topic && value)
|
||||
_handlePublish(baseFolder, itemList, topic, value);
|
||||
else {
|
||||
DBG_ERROR(NULL, "Either topic or value missing in PUBLISH msg");
|
||||
}
|
||||
free(value);
|
||||
free(topic);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handlePublish(const char *baseFolder, const ITEM_LIST *itemList, const char *topic, const char *value)
|
||||
{
|
||||
const ITEM *item;
|
||||
|
||||
item=_getItemForTopic(itemList, topic);
|
||||
if (item) {
|
||||
const char *t;
|
||||
|
||||
DBG_INFO(AQH_LOGDOMAIN, "HANDLING topic \"%s\"", topic);
|
||||
t=Item_GetDataType(item);
|
||||
if (t && strcasecmp(t, "json")==0)
|
||||
_handleJsonMsgForItem(baseFolder, item, value);
|
||||
else
|
||||
_handleRawMsgForItem(baseFolder, item, value);
|
||||
}
|
||||
else {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "ignoring topic \"%s\"", topic);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleJsonMsgForItem(const char *baseFolder, const ITEM *item, const char *value)
|
||||
{
|
||||
GWEN_JSON_ELEM *jeRoot;
|
||||
|
||||
jeRoot=GWEN_JsonElement_fromString(value);
|
||||
if (jeRoot==NULL) {
|
||||
DBG_INFO(NULL, "Could not parse JSON value: %s", value);
|
||||
}
|
||||
else {
|
||||
const ITEMVAR_LIST *itemVarList;
|
||||
|
||||
itemVarList=Item_GetVarList(item);
|
||||
if (itemVarList) {
|
||||
ITEMVAR *itemVar;
|
||||
|
||||
itemVar=ItemVar_List_First(itemVarList);
|
||||
while(itemVar) {
|
||||
_handleJsonItemVar(baseFolder, item, itemVar, jeRoot);
|
||||
itemVar=ItemVar_List_Next(itemVar);
|
||||
}
|
||||
}
|
||||
GWEN_JsonElement_free(jeRoot);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleJsonItemVar(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, GWEN_JSON_ELEM *jeRoot)
|
||||
{
|
||||
const char *path;
|
||||
|
||||
path=ItemVar_GetPath(itemVar);
|
||||
if (path) {
|
||||
GWEN_JSON_ELEM *je;
|
||||
|
||||
je=GWEN_JsonElement_GetElementByPath(jeRoot, path, 0);
|
||||
if (je) {
|
||||
const char *s;
|
||||
|
||||
s=GWEN_JsonElement_GetData(je);
|
||||
if (s && *s)
|
||||
_writeValueAccordingToItem(baseFolder, item, itemVar, s);
|
||||
else {
|
||||
DBG_ERROR(NULL, "Path \"%s\" in JSON data contains no data", path);
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Path \"%s\" not found in JSON data", path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleRawMsgForItem(const char *baseFolder, const ITEM *item, const char *value)
|
||||
{
|
||||
const ITEMVAR_LIST *itemVarList;
|
||||
|
||||
itemVarList=Item_GetVarList(item);
|
||||
if (itemVarList) {
|
||||
ITEMVAR *itemVar;
|
||||
|
||||
itemVar=ItemVar_List_First(itemVarList);
|
||||
while(itemVar) {
|
||||
_writeValueAccordingToItem(baseFolder, item, itemVar, value);
|
||||
itemVar=ItemVar_List_Next(itemVar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _writeValueAccordingToItem(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, const char *value)
|
||||
{
|
||||
const char *id;
|
||||
const char *name;
|
||||
|
||||
id=Item_GetId(item);
|
||||
name=ItemVar_GetName(itemVar);
|
||||
if (id && *id && name && *name) {
|
||||
GWEN_BUFFER *fnameBuf;
|
||||
|
||||
fnameBuf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
GWEN_Buffer_AppendString(fnameBuf, baseFolder);
|
||||
GWEN_Buffer_AppendString(fnameBuf, GWEN_DIR_SEPARATOR_S);
|
||||
GWEN_Buffer_AppendArgs(fnameBuf, "%s_%s", id, name);
|
||||
_writeToFile(GWEN_Buffer_GetStart(fnameBuf), value);
|
||||
GWEN_Buffer_free(fnameBuf);
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Either id or name missing in item list file");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _writeToFile(const char *filename, const char *txt)
|
||||
{
|
||||
if (txt && *txt) {
|
||||
GWEN_BUFFER *tmpNameBuf;
|
||||
int rv;
|
||||
|
||||
tmpNameBuf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
GWEN_Buffer_AppendString(tmpNameBuf, filename);
|
||||
GWEN_Buffer_AppendString(tmpNameBuf, ".tmp");
|
||||
|
||||
rv=GWEN_Directory_GetPath(GWEN_Buffer_GetStart(tmpNameBuf), GWEN_PATH_FLAGS_VARIABLE);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Error getting path for %s (%d)", GWEN_Buffer_GetStart(tmpNameBuf), rv);
|
||||
}
|
||||
else {
|
||||
FILE *f;
|
||||
|
||||
f=fopen(GWEN_Buffer_GetStart(tmpNameBuf), "w");
|
||||
if (f) {
|
||||
if (1!=fwrite(txt, strlen(txt), 1, f)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error writing.");
|
||||
fclose(f);
|
||||
}
|
||||
else {
|
||||
fclose(f);
|
||||
rename(GWEN_Buffer_GetStart(tmpNameBuf), filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
GWEN_Buffer_free(tmpNameBuf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
const ITEM *_getItemForTopic(const ITEM_LIST *itemList, const char *topic)
|
||||
{
|
||||
const ITEM *item;
|
||||
|
||||
item=Item_List_First(itemList);
|
||||
while(item) {
|
||||
const char *s;
|
||||
|
||||
s=Item_GetTopic(item);
|
||||
if (s && GWEN_Text_ComparePattern(topic, s, 0)!=-1)
|
||||
return item;
|
||||
item=Item_List_Next(item);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef AQHOME_TOOL_MESSAGES_H
|
||||
#define AQHOME_TOOL_MESSAGES_H
|
||||
|
||||
|
||||
#include "./item.h"
|
||||
|
||||
#include <gwenhywfar/msg.h>
|
||||
|
||||
|
||||
ITEM_LIST *AqHomeMqttLog_ReadItems(GWEN_DB_NODE *dbArgs);
|
||||
void AqHomeMqttLog_HandlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "./mqtt.h"
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/aqhome.h>
|
||||
|
||||
#include "aqhome/mqtt/endpoint_mqttc.h"
|
||||
#include <aqhome/mqtt/msg_mqtt_connect.h>
|
||||
#include <aqhome/mqtt/msg_mqtt_connack.h>
|
||||
#include <aqhome/mqtt/msg_mqtt_publish.h>
|
||||
#include <aqhome/mqtt/msg_mqtt_subscribe.h>
|
||||
#include <aqhome/mqtt/msg_mqtt_suback.h>
|
||||
|
||||
#include <gwenhywfar/endpoint_multilayer.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* defines
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define AQHOME_MQTTLOG_DEFAULT_CMDTIMEOUT 10000
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT *epTcp, uint8_t expectedPacketType, int timeoutInSeconds);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
GWEN_MSG_ENDPOINT *AqHomeMqttLog_CreateMqttEndpoint(GWEN_DB_NODE *dbArgs)
|
||||
{
|
||||
const char *mqttAddress;
|
||||
int mqttPort;
|
||||
const char *mqttClientId;
|
||||
int mqttKeepAlive;
|
||||
|
||||
mqttAddress=GWEN_DB_GetCharValue(dbArgs, "mqttAddress", 0, NULL);
|
||||
mqttPort=GWEN_DB_GetIntValue(dbArgs, "mqttPort", 0, 1883);
|
||||
mqttClientId=GWEN_DB_GetCharValue(dbArgs, "mqttClientId", 0, "aqhome-mqttlog");
|
||||
mqttKeepAlive=GWEN_DB_GetIntValue(dbArgs, "mqttKeepAlive", 0, 600);
|
||||
|
||||
if (mqttAddress && *mqttAddress && mqttPort) {
|
||||
GWEN_MSG_ENDPOINT *epMqtt;
|
||||
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Connecting to %s (port %d)", mqttAddress, mqttPort);
|
||||
epMqtt=AQH_MqttClientEndpoint_new(mqttClientId, mqttAddress, mqttPort, NULL, 0);
|
||||
if (epMqtt==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint TCP");
|
||||
return NULL;
|
||||
}
|
||||
AQH_MqttClientEndpoint_SetKeepAliveTime(epMqtt, mqttKeepAlive);
|
||||
GWEN_MsgEndpoint_AddFlags(epMqtt, AQH_ENDPOINT2_MQTTCLIENT_FLAGS_SUBSCRIBEALL);
|
||||
|
||||
return epMqtt;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
int AqHomeMqttLog_MqttConnect(GWEN_MSG_ENDPOINT *epTcp)
|
||||
{
|
||||
if (GWEN_MsgEndpoint_GetState(epTcp)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
|
||||
int rv;
|
||||
|
||||
rv=GWEN_MultilayerEndpoint_StartConnect(epTcp);
|
||||
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
|
||||
DBG_ERROR(NULL, "Error starting to connect (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
while(GWEN_MsgEndpoint_GetState(epTcp)!=GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
|
||||
DBG_DEBUG(NULL, "Next loop");
|
||||
GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AqHomeMqttLog_Subscribe(GWEN_MSG_ENDPOINT *epTcp, const char *topicFilter)
|
||||
{
|
||||
uint16_t pckId;
|
||||
GWEN_MSG *msgOut;
|
||||
GWEN_MSG *msgIn;
|
||||
|
||||
DBG_INFO(NULL, "Sending SUBSCRIBE %s", topicFilter);
|
||||
pckId=AQH_MqttClientEndpoint_GetNextPacketId(epTcp);
|
||||
msgOut=GWEN_SubscribeMqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE, pckId, topicFilter, 0);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
return GWEN_ERROR_INTERNAL;
|
||||
}
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
|
||||
DBG_INFO(NULL, "Waiting for response");
|
||||
msgIn=_awaitPacket(epTcp, AQH_MQTTMSG_MSGTYPE_SUBACK, AQHOME_MQTTLOG_DEFAULT_CMDTIMEOUT);
|
||||
if (msgIn) {
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
AQH_SubAckMqttMsg_DumpToBuffer(msgIn, buf, "received");
|
||||
DBG_INFO(NULL, "%s", GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
GWEN_Msg_free(msgIn);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
int AqHomeMqttLog_Ping(GWEN_MSG_ENDPOINT *epTcp)
|
||||
{
|
||||
GWEN_MSG *msgOut;
|
||||
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Sending PING");
|
||||
msgOut=GWEN_MqttMsg_new(AQH_MQTTMSG_MSGTYPE_PINGREQ, 0, NULL);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
return GWEN_ERROR_INTERNAL;
|
||||
}
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT *epTcp, uint8_t expectedPacketType, int timeoutInSeconds)
|
||||
{
|
||||
time_t startTime;
|
||||
|
||||
startTime=time(NULL);
|
||||
|
||||
for (;;) {
|
||||
GWEN_MSG *msg;
|
||||
time_t now;
|
||||
|
||||
GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */
|
||||
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
|
||||
if (msg) {
|
||||
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(expectedPacketType & 0xf0)) {
|
||||
return msg;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Received this message:");
|
||||
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
|
||||
}
|
||||
GWEN_Msg_free(msg);
|
||||
}
|
||||
now=time(NULL);
|
||||
if (now-startTime>timeoutInSeconds) {
|
||||
DBG_INFO(NULL, "Timeout");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef AQHOME_TOOL_MQTT_H
|
||||
#define AQHOME_TOOL_MQTT_H
|
||||
|
||||
|
||||
#include "aqhome/mqtt/endpoint_mqttc.h"
|
||||
|
||||
#include <gwenhywfar/db.h>
|
||||
|
||||
|
||||
|
||||
GWEN_MSG_ENDPOINT *AqHomeMqttLog_CreateMqttEndpoint(GWEN_DB_NODE *dbArgs);
|
||||
/*int AqHomeMqttLog_MqttConnect(GWEN_MSG_ENDPOINT *epTcp); */
|
||||
/* int AqHomeMqttLog_Subscribe(GWEN_MSG_ENDPOINT *epTcp, const char *topicFilter); */
|
||||
int AqHomeMqttLog_Ping(GWEN_MSG_ENDPOINT *epTcp);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -56,11 +56,11 @@ static AQHMQTT_VALUE *_readXmlValue(AQHOME_MQTT *aqh, GWEN_XMLNODE *valueNode);
|
||||
*/
|
||||
|
||||
|
||||
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadSysconfDeviceFiles(AQHOME_MQTT *aqh)
|
||||
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadRuntimeDataDeviceFiles(AQHOME_MQTT *aqh)
|
||||
{
|
||||
GWEN_STRINGLIST *sl;
|
||||
|
||||
sl=AQH_GetListOfMatchingSysconfFiles("devices", "*.xml");
|
||||
sl=AQH_GetListOfMatchingRuntimeDataFiles("aqhome/devices", "*.xml");
|
||||
if (sl) {
|
||||
AQHMQTT_DEVICE_LIST *deviceList;
|
||||
|
||||
@@ -80,24 +80,24 @@ AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadSysconfDeviceFiles(AQHOME_MQTT *aqh)
|
||||
|
||||
|
||||
|
||||
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadRuntimeDataDeviceFiles(AQHOME_MQTT *aqh)
|
||||
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDataDeviceFiles(AQHOME_MQTT *aqh)
|
||||
{
|
||||
GWEN_STRINGLIST *sl;
|
||||
|
||||
sl=AQH_GetListOfMatchingRuntimeDataFiles("aqhome-mqtt/devices", "*.xml");
|
||||
sl=AQH_GetListOfMatchingDataFiles("aqhome/devices", "*.xml");
|
||||
if (sl) {
|
||||
AQHMQTT_DEVICE_LIST *deviceList;
|
||||
|
||||
deviceList=_readDeviceFiles(aqh, sl);
|
||||
GWEN_StringList_free(sl);
|
||||
if (deviceList==NULL) {
|
||||
DBG_INFO(NULL, "Error reading sysconf device files");
|
||||
DBG_INFO(NULL, "Error reading data device files");
|
||||
return NULL;
|
||||
}
|
||||
return deviceList;
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "No sysconf device files");
|
||||
DBG_ERROR(NULL, "No data device files");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -119,8 +119,10 @@ AQHMQTT_DEVICE_LIST *_readDeviceFiles(AQHOME_MQTT *aqh, const GWEN_STRINGLIST *s
|
||||
AQHMQTT_DEVICE *device;
|
||||
|
||||
device=_readDeviceFile(aqh, s);
|
||||
if (device)
|
||||
AQHMQTT_Device_List_Add(device, deviceList);
|
||||
if (device) {
|
||||
DBG_ERROR(NULL, "Adding device \"%s\" to list", AQHMQTT_Device_GetName(device));
|
||||
AQHMQTT_Device_List_Add(device, deviceList);
|
||||
}
|
||||
}
|
||||
se=GWEN_StringListEntry_Next(se);
|
||||
}
|
||||
@@ -151,15 +153,23 @@ AQHMQTT_DEVICE *_readDeviceFile(AQHOME_MQTT *aqh, const char *sFilename)
|
||||
}
|
||||
deviceNode=GWEN_XMLNode_FindFirstTag(rootNode, "device", NULL, NULL);
|
||||
if (deviceNode) {
|
||||
AQHMQTT_DEVICE *device;
|
||||
const char *driverName;
|
||||
|
||||
device=_readXmlDevice(aqh, deviceNode);
|
||||
GWEN_XMLNode_free(rootNode);
|
||||
if (device==NULL) {
|
||||
DBG_INFO(NULL, "Error reading device from XML file \"%s\" (%d)", sFilename, rv);
|
||||
return NULL;
|
||||
driverName=GWEN_XMLNode_GetProperty(deviceNode, "driver", NULL);
|
||||
if (driverName && *driverName && strcasecmp(driverName, "mqtt")==0) {
|
||||
AQHMQTT_DEVICE *device;
|
||||
|
||||
device=_readXmlDevice(aqh, deviceNode);
|
||||
GWEN_XMLNode_free(rootNode);
|
||||
if (device==NULL) {
|
||||
DBG_INFO(NULL, "Error reading device from XML file \"%s\" (%d)", sFilename, rv);
|
||||
return NULL;
|
||||
}
|
||||
return device;
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "Device is not an MQTT device, ignoring");
|
||||
}
|
||||
return device;
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "XML file \"%s\" does not contain a <device> element", sFilename);
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
|
||||
|
||||
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadSysconfDeviceFiles(AQHOME_MQTT *aqh);
|
||||
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDataDeviceFiles(AQHOME_MQTT *aqh);
|
||||
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadRuntimeDataDeviceFiles(AQHOME_MQTT *aqh);
|
||||
|
||||
|
||||
|
||||
@@ -179,7 +179,13 @@ void _setupBroker(AQHOMED *aqh, GWEN_DB_NODE *dbArgs)
|
||||
const char *brokerClientId;
|
||||
|
||||
brokerAddress=GWEN_DB_GetCharValue(dbArgs, "brokerAddress", 0, NULL);
|
||||
brokerPort=GWEN_DB_GetIntValue(dbArgs, "brokerPort", 0, AQHOMED_DEFAULT_BROKER_PORT);
|
||||
if (!(brokerAddress && *brokerAddress))
|
||||
brokerAddress=GWEN_DB_GetCharValue(dbArgs, "ConfigFile/brokerAddress", 0, "127.0.0.1");
|
||||
|
||||
brokerPort=GWEN_DB_GetIntValue(dbArgs, "brokerPort", 0, -1);
|
||||
if (brokerPort<0)
|
||||
brokerPort=GWEN_DB_GetIntValue(dbArgs, "ConfigFile/brokerPort", 0, AQHOMED_DEFAULT_BROKER_PORT);
|
||||
|
||||
brokerClientId=GWEN_DB_GetCharValue(dbArgs, "brokerClientId", 0, AQHOMED_DEFAULT_BROKER_CLIENTID);
|
||||
|
||||
if (brokerAddress && *brokerAddress && brokerPort) {
|
||||
|
||||
Reference in New Issue
Block a user