adapt to latest changes, use generic request handlers.

This commit is contained in:
Martin Preuss
2025-09-16 23:09:18 +02:00
parent 8805712aea
commit b0ce010dc8
3 changed files with 188 additions and 186 deletions

View File

@@ -51,18 +51,22 @@ void AQH_ModDataClient_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFo
int AQH_ModDataClient_HandleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_MODDATACLIENT_RUN_FN runFn) void AQH_ModDataClient_HandleRequest(AQH_MODULE *m,
AQCGI_REQUEST *rq,
AQH_SESSION *session,
AQH_MODDATACLIENT_RUN_FN runFn,
GWEN_BUFFER *dbuf)
{ {
AQH_EVENT_LOOP *eventLoop; AQH_EVENT_LOOP *eventLoop;
AQH_DATACLIENT *dc; AQH_DATACLIENT *dc;
GWEN_BUFFER *dbuf;
int rv; int rv;
rv=AQH_Init(); rv=AQH_Init();
if (rv<0) { if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv); DBG_ERROR(NULL, "here (%d)", rv);
AQCGI_SendResponseWithStatus(rq, 500, "Internal Error"); AQCGI_Request_SetResponseCode(rq, 500);
return rv; AQCGI_Request_SetResponseText(rq, "Internal Error");
return;
} }
eventLoop=AQH_EventLoop_new(); eventLoop=AQH_EventLoop_new();
@@ -70,9 +74,9 @@ int AQH_ModDataClient_HandleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSIO
rv=AQH_DataClient_ReadConfigFile(dc); rv=AQH_DataClient_ReadConfigFile(dc);
if (rv<0) { if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv); DBG_ERROR(NULL, "here (%d)", rv);
AQCGI_SendResponseWithStatus(rq, 500, "Internal Error"); AQCGI_Request_SetResponseCode(rq, 500);
AQH_Session_free(session); AQCGI_Request_SetResponseText(rq, "Internal Error");
return rv; return;
} }
rv=AQH_DataClient_ConnectWithArgs(dc, 0); rv=AQH_DataClient_ConnectWithArgs(dc, 0);
@@ -80,25 +84,18 @@ int AQH_ModDataClient_HandleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSIO
DBG_ERROR(NULL, "Error connecting (%d)", rv); DBG_ERROR(NULL, "Error connecting (%d)", rv);
AQH_DataClient_free(dc); AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop); AQH_EventLoop_free(eventLoop);
AQCGI_SendResponseWithStatus(rq, 500, "Internal Error"); AQCGI_Request_SetResponseCode(rq, 500);
AQH_Session_free(session); AQCGI_Request_SetResponseText(rq, "Internal Error");
return GWEN_ERROR_GENERIC; return;
} }
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
AQH_ModService_AddHeader(m, "en", dbuf);
if (runFn) if (runFn)
runFn(m, rq, session, dc, dbuf); runFn(m, rq, session, dc, dbuf);
AQH_ModService_AddFooter(m, "en", dbuf);
AQCGI_Request_SetBufferResponseBody(rq, dbuf);
AQCGI_Request_AddResponseHeaderData(rq, "Content-type: text/html");
AQH_DataClient_free(dc); AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop); AQH_EventLoop_free(eventLoop);
AQH_Session_free(session);
AQH_Fini(); AQH_Fini();
return AQCGI_SendResponse(rq);
} }

View File

@@ -24,7 +24,11 @@ typedef void (*AQH_MODDATACLIENT_RUN_FN)(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_S
void AQH_ModDataClient_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolder); void AQH_ModDataClient_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolder);
int AQH_ModDataClient_HandleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_MODDATACLIENT_RUN_FN runFn); void AQH_ModDataClient_HandleRequest(AQH_MODULE *m,
AQCGI_REQUEST *rq,
AQH_SESSION *session,
AQH_MODDATACLIENT_RUN_FN runFn,
GWEN_BUFFER *dbuf);

View File

@@ -27,10 +27,10 @@
* ------------------------------------------------------------------------------------------------ * ------------------------------------------------------------------------------------------------
*/ */
/* ------------------------------------------------------------------------------------------------ #define GBAS GWEN_Buffer_AppendString
* global vars #define GBAA GWEN_Buffer_AppendArgs
* ------------------------------------------------------------------------------------------------
*/
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* forward declarations * forward declarations
@@ -42,10 +42,28 @@ static void _createRoleList(AQH_MODULE *m);
static AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sModuleName); 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 int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem);
static void _handleRqIndexGet(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf);
static void _handleRqValuesGet(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf);
static void _runIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf); 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); static void _runValues(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf);
/* ------------------------------------------------------------------------------------------------
* vars
* ------------------------------------------------------------------------------------------------
*/
static AQH_MODSERVICE_HANDLER_ENTRY _requestTable[]={
{"index.html", AQCGI_REQUEST_METHOD_GET, AQH_MODDEVICES_PERMS_DEVICEREAD, _handleRqIndexGet},
{"values.html", AQCGI_REQUEST_METHOD_GET, AQH_MODDEVICES_PERMS_DEVICEREAD | AQH_MODDEVICES_PERMS_VALUEREAD, _handleRqValuesGet},
{NULL, 0, 0, NULL}
};
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* code * code
* ------------------------------------------------------------------------------------------------ * ------------------------------------------------------------------------------------------------
@@ -145,189 +163,172 @@ AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *sessio
int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem) int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem)
{ {
if (strcasecmp(sLastPathElem, "index.html")==0) AQH_ModService_HandleRequestWithTable(m, rq, session, sLastPathElem, _requestTable);
return AQH_ModDataClient_HandleRequest(m, rq, session, _runIndex); return AQCGI_SendResponse(rq);
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 _handleRqIndexGet(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf)
} {
AQH_ModDataClient_HandleRequest(m, rq, session, _runIndex, dbuf);
}
void _handleRqValuesGet(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf)
{
AQH_ModDataClient_HandleRequest(m, rq, session, _runValues, dbuf);
} }
void _runIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf) void _runIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
{ {
uint32_t perms; AQH_DEVICE_LIST *deviceList;
AQH_DEVICE *device;
perms=AQH_ModService_GetUserPerms(m); deviceList=AQH_DataClient_GetDevices(dc, NULL);
DBG_ERROR(NULL, "Perms=%08x", perms); if (deviceList==NULL) {
if (perms & AQH_MODDEVICES_PERMS_DEVICEREAD) { DBG_ERROR(NULL, "No device received");
AQH_DEVICE_LIST *deviceList; GBAS(dbuf, "Empty device list.");
AQH_DEVICE *device; GBAS(dbuf, "<p>No devices.</p>");
deviceList=AQH_DataClient_GetDevices(dc, NULL);
if (deviceList==NULL) {
DBG_ERROR(NULL, "No device received");
GWEN_Buffer_AppendString(dbuf, "Empty device list.");
GWEN_Buffer_AppendString(dbuf, "<p>No devices.</p>");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
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);
AQCGI_Request_SetResponseCode(rq, 200); AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok"); AQCGI_Request_SetResponseText(rq, "Ok");
return;
} }
else {
GWEN_Buffer_AppendString(dbuf, "<p>No permissions to read devices.</p>"); GBAS(dbuf, "<h1>Devices</h1>\n");
AQCGI_Request_SetResponseCode(rq, 200); GBAS(dbuf,
AQCGI_Request_SetResponseText(rq, "Ok"); "<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;
GBAA(dbuf, "<tr>");
/* name for system */
s=AQH_Device_GetNameForSystem(device);
GBAS(dbuf,"<td><a href=\"values.html?device=");
GWEN_Text_EscapeToBufferTolerant(s, dbuf);
GBAA(dbuf,"\">%s</a></td>", s?s:"");
/* room */
s=AQH_Device_GetRoomName(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
/* location */
s=AQH_Device_GetLocation(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
/* description */
s=AQH_Device_GetDescription(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
/* device type */
s=AQH_Device_GetDeviceType(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
/* driver */
s=AQH_Device_GetDriver(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
/* short device name */
s=AQH_Device_GetName(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
/* GUI name for device */
s=AQH_Device_GetNameForGui(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
/* manufacturer */
s=AQH_Device_GetManufacturer(device);
GBAA(dbuf, "<td>%s</td>", s?s:"");
GBAA(dbuf, "</tr>");
device=AQH_Device_List_Next(device);
} }
GBAS(dbuf,
"</tbody>\n"
"</table>\n");
AQH_Device_List_free(deviceList);
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
} }
void _runValues(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf) void _runValues(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
{ {
uint32_t perms; GWEN_DB_NODE *dbQuery;
const char *sDeviceName;
perms=AQH_ModService_GetUserPerms(m); dbQuery=AQCGI_Request_GetDbQuery(rq);
DBG_ERROR(NULL, "Perms=%08x", perms); sDeviceName=GWEN_DB_GetCharValue(dbQuery, "device", 0, NULL);
if ((perms & AQH_MODDEVICES_PERMS_DEVICEREAD) && if (!(sDeviceName && *sDeviceName))
(perms & AQH_MODDEVICES_PERMS_VALUEREAD)) { _runIndex(m, rq, session, dc, 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>Modality</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_ValueModality_toString(AQH_Value_GetModality(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);
}
}
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
else { else {
GWEN_Buffer_AppendString(dbuf, "<p>No permissions to read devices/values.</p>"); AQH_VALUE_LIST *valueList;
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok"); valueList=AQH_DataClient_GetValues(dc, sDeviceName, 0);
if (valueList) {
AQH_VALUE *value;
GBAA(dbuf, "<h1>Values for Device %s</h1>\n", sDeviceName);
GBAS(dbuf,
"<table class=\"datatable\">\n"
"<thead>"
"<tr>"
"<th>Name</th>"
"<th>Type</th>"
"<th>Modality</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;
GBAS(dbuf, "<tr>");
s=AQH_Value_GetName(value);
GBAA(dbuf, "<td>%s</td>", s?s:"");
s=AQH_ValueType_toString(AQH_Value_GetValueType(value));
GBAA(dbuf, "<td>%s</td>", s?s:"");
s=AQH_ValueModality_toString(AQH_Value_GetModality(value));
GBAA(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetDriver(value);
GBAA(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetDeviceNameForSystem(value);
GBAA(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetNameForSystem(value);
GBAA(dbuf, "<td>%s</td>", s?s:"");
GBAA(dbuf, "</tr>\n");
value=AQH_Value_List_Next(value);
}
GBAS(dbuf,
"</tbody>\n"
"</table>\n");
AQH_Value_List_free(valueList);
}
} }
} }