More work on aqhome-cgi.
listing devices and values works.
This commit is contained in:
229
apps/aqhome-cgi/modules/mdevices.c
Normal file
229
apps/aqhome-cgi/modules/mdevices.c
Normal file
@@ -0,0 +1,229 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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 "./mroot_p.h"
|
||||
#include "./mdevices.h"
|
||||
|
||||
#include "aqhome-cgi/service/module.h"
|
||||
#include "aqhome-cgi/modules/mdataclient.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
#include <gwenhywfar/timestamp.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* defs and enums
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* global vars
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sModuleName);
|
||||
static int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem);
|
||||
static void _runIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf);
|
||||
static void _runValues(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf);
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* code
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
void AQH_ModDevices_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolder)
|
||||
{
|
||||
AQH_ModService_Extend(m, sv, baseFolder);
|
||||
AQH_ModService_SetHandleRequestFn(m, _handleRequest);
|
||||
AQH_ModService_SetLoadSubModuleFn(m, _loadSubModule);
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sModuleName)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem)
|
||||
{
|
||||
if (strcasecmp(sLastPathElem, "index.html")==0)
|
||||
return AQH_ModDataClient_HandleRequest(m, rq, session, _runIndex);
|
||||
else if (strcasecmp(sLastPathElem, "values.html")==0)
|
||||
return AQH_ModDataClient_HandleRequest(m, rq, session, _runValues);
|
||||
else {
|
||||
AQCGI_SendResponseWithStatus(rq, 404, "Not Found");
|
||||
return GWEN_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _runIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
|
||||
{
|
||||
AQH_DEVICE_LIST *deviceList;
|
||||
AQH_DEVICE *device;
|
||||
|
||||
deviceList=AQH_DataClient_GetDevices(dc, NULL);
|
||||
if (deviceList==NULL) {
|
||||
DBG_ERROR(NULL, "No device received");
|
||||
GWEN_Buffer_AppendString(dbuf, "Empty device list.");
|
||||
return;
|
||||
}
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf, "<h1>Devices</h1>\n");
|
||||
GWEN_Buffer_AppendString(dbuf,
|
||||
"<table class=\"datatable\">\n"
|
||||
"<thead>\n"
|
||||
"<tr>"
|
||||
"<th>Name For System</th>"
|
||||
"<th>Room</th>"
|
||||
"<th>Location</th>"
|
||||
"<th>Description</th>"
|
||||
"<th>Type</th>"
|
||||
"<th>Driver</th>"
|
||||
"<th>Name</th>"
|
||||
"<th>GUI Name</th>"
|
||||
"<th>Manufacturer</th>"
|
||||
"</tr>\n"
|
||||
"</thead>\n"
|
||||
"<tbody>\n");
|
||||
|
||||
device=AQH_Device_List_First(deviceList);
|
||||
while(device) {
|
||||
const char *s;
|
||||
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<tr>");
|
||||
/* name for system */
|
||||
s=AQH_Device_GetNameForSystem(device);
|
||||
GWEN_Buffer_AppendString(dbuf,"<td><a href=\"values.html?device=");
|
||||
GWEN_Text_EscapeToBufferTolerant(s, dbuf);
|
||||
GWEN_Buffer_AppendArgs(dbuf,"\">%s</a></td>", s?s:"");
|
||||
/* room */
|
||||
s=AQH_Device_GetRoomName(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* location */
|
||||
s=AQH_Device_GetLocation(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* description */
|
||||
s=AQH_Device_GetDescription(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* device type */
|
||||
s=AQH_Device_GetDeviceType(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* driver */
|
||||
s=AQH_Device_GetDriver(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* short device name */
|
||||
s=AQH_Device_GetName(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* GUI name for device */
|
||||
s=AQH_Device_GetNameForGui(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* manufacturer */
|
||||
s=AQH_Device_GetManufacturer(device);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
|
||||
GWEN_Buffer_AppendArgs(dbuf, "</tr>");
|
||||
device=AQH_Device_List_Next(device);
|
||||
}
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf,
|
||||
"</tbody>\n"
|
||||
"</table>\n");
|
||||
|
||||
AQH_Device_List_free(deviceList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _runValues(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
|
||||
{
|
||||
GWEN_DB_NODE *dbQuery;
|
||||
const char *sDeviceName;
|
||||
|
||||
dbQuery=AQCGI_Request_GetDbQuery(rq);
|
||||
sDeviceName=GWEN_DB_GetCharValue(dbQuery, "device", 0, NULL);
|
||||
if (!(sDeviceName && *sDeviceName))
|
||||
_runIndex(m, rq, session, dc, dbuf);
|
||||
else {
|
||||
AQH_VALUE_LIST *valueList;
|
||||
|
||||
valueList=AQH_DataClient_GetValues(dc, sDeviceName, 0);
|
||||
if (valueList) {
|
||||
AQH_VALUE *value;
|
||||
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<h1>Values for Device %s</h1>\n", sDeviceName);
|
||||
GWEN_Buffer_AppendString(dbuf,
|
||||
"<table class=\"datatable\">\n"
|
||||
"<thead>"
|
||||
"<tr>"
|
||||
"<th>Name</th>"
|
||||
"<th>Type</th>"
|
||||
"<th>Driver</th>"
|
||||
"<th>Device</th>"
|
||||
"<th>Name for System</th>"
|
||||
"</tr>"
|
||||
"</thead>\n"
|
||||
"<tbody>\n");
|
||||
|
||||
value=AQH_Value_List_First(valueList);
|
||||
while(value) {
|
||||
const char *s;
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf, "<tr>");
|
||||
|
||||
s=AQH_Value_GetName(value);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
|
||||
s=AQH_ValueType_toString(AQH_Value_GetValueType(value));
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
|
||||
s=AQH_Value_GetDriver(value);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
|
||||
s=AQH_Value_GetDeviceNameForSystem(value);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
|
||||
s=AQH_Value_GetNameForSystem(value);
|
||||
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
|
||||
|
||||
GWEN_Buffer_AppendArgs(dbuf, "</tr>\n");
|
||||
|
||||
value=AQH_Value_List_Next(value);
|
||||
}
|
||||
GWEN_Buffer_AppendString(dbuf,
|
||||
"</tbody>\n"
|
||||
"</table>\n");
|
||||
AQH_Value_List_free(valueList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user