diff --git a/apps/aqhome-cgi/modules/mdataclient.c b/apps/aqhome-cgi/modules/mdataclient.c index bd7457e..fbc47eb 100644 --- a/apps/aqhome-cgi/modules/mdataclient.c +++ b/apps/aqhome-cgi/modules/mdataclient.c @@ -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_DATACLIENT *dc; - GWEN_BUFFER *dbuf; int rv; rv=AQH_Init(); if (rv<0) { DBG_ERROR(NULL, "here (%d)", rv); - AQCGI_SendResponseWithStatus(rq, 500, "Internal Error"); - return rv; + AQCGI_Request_SetResponseCode(rq, 500); + AQCGI_Request_SetResponseText(rq, "Internal Error"); + return; } 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); if (rv<0) { DBG_ERROR(NULL, "here (%d)", rv); - AQCGI_SendResponseWithStatus(rq, 500, "Internal Error"); - AQH_Session_free(session); - return rv; + AQCGI_Request_SetResponseCode(rq, 500); + AQCGI_Request_SetResponseText(rq, "Internal Error"); + return; } 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); AQH_DataClient_free(dc); AQH_EventLoop_free(eventLoop); - AQCGI_SendResponseWithStatus(rq, 500, "Internal Error"); - AQH_Session_free(session); - return GWEN_ERROR_GENERIC; + AQCGI_Request_SetResponseCode(rq, 500); + AQCGI_Request_SetResponseText(rq, "Internal Error"); + return; } - dbuf=GWEN_Buffer_new(0, 256, 0, 1); - AQH_ModService_AddHeader(m, "en", dbuf); if (runFn) 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_EventLoop_free(eventLoop); - AQH_Session_free(session); AQH_Fini(); - return AQCGI_SendResponse(rq); } diff --git a/apps/aqhome-cgi/modules/mdataclient.h b/apps/aqhome-cgi/modules/mdataclient.h index f84322f..7efad96 100644 --- a/apps/aqhome-cgi/modules/mdataclient.h +++ b/apps/aqhome-cgi/modules/mdataclient.h @@ -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); -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); diff --git a/apps/aqhome-cgi/modules/mdevices.c b/apps/aqhome-cgi/modules/mdevices.c index 3fbe51f..434d1d2 100644 --- a/apps/aqhome-cgi/modules/mdevices.c +++ b/apps/aqhome-cgi/modules/mdevices.c @@ -27,10 +27,10 @@ * ------------------------------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------------------------------ - * global vars - * ------------------------------------------------------------------------------------------------ - */ +#define GBAS GWEN_Buffer_AppendString +#define GBAA GWEN_Buffer_AppendArgs + + /* ------------------------------------------------------------------------------------------------ * 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 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 _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 * ------------------------------------------------------------------------------------------------ @@ -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) { - 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; - } + AQH_ModService_HandleRequestWithTable(m, rq, session, sLastPathElem, _requestTable); + return AQCGI_SendResponse(rq); +} + + + +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) { - uint32_t perms; + AQH_DEVICE_LIST *deviceList; + AQH_DEVICE *device; - perms=AQH_ModService_GetUserPerms(m); - DBG_ERROR(NULL, "Perms=%08x", perms); - if (perms & AQH_MODDEVICES_PERMS_DEVICEREAD) { - 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."); - GWEN_Buffer_AppendString(dbuf, "

No devices.

"); - AQCGI_Request_SetResponseCode(rq, 200); - AQCGI_Request_SetResponseText(rq, "Ok"); - return; - } - - GWEN_Buffer_AppendString(dbuf, "

Devices

\n"); - GWEN_Buffer_AppendString(dbuf, - "\n" - "\n" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "\n" - "\n" - "\n"); - - device=AQH_Device_List_First(deviceList); - while(device) { - const char *s; - - GWEN_Buffer_AppendArgs(dbuf, ""); - /* name for system */ - s=AQH_Device_GetNameForSystem(device); - GWEN_Buffer_AppendString(dbuf,"", s?s:""); - /* room */ - s=AQH_Device_GetRoomName(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - /* location */ - s=AQH_Device_GetLocation(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - /* description */ - s=AQH_Device_GetDescription(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - /* device type */ - s=AQH_Device_GetDeviceType(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - /* driver */ - s=AQH_Device_GetDriver(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - /* short device name */ - s=AQH_Device_GetName(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - /* GUI name for device */ - s=AQH_Device_GetNameForGui(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - /* manufacturer */ - s=AQH_Device_GetManufacturer(device); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - - GWEN_Buffer_AppendArgs(dbuf, ""); - device=AQH_Device_List_Next(device); - } - - GWEN_Buffer_AppendString(dbuf, - "\n" - "
Name For SystemRoomLocationDescriptionTypeDriverNameGUI NameManufacturer
%s%s%s%s%s%s%s%s%s
\n"); - AQH_Device_List_free(deviceList); + deviceList=AQH_DataClient_GetDevices(dc, NULL); + if (deviceList==NULL) { + DBG_ERROR(NULL, "No device received"); + GBAS(dbuf, "Empty device list."); + GBAS(dbuf, "

No devices.

"); AQCGI_Request_SetResponseCode(rq, 200); AQCGI_Request_SetResponseText(rq, "Ok"); + return; } - else { - GWEN_Buffer_AppendString(dbuf, "

No permissions to read devices.

"); - AQCGI_Request_SetResponseCode(rq, 200); - AQCGI_Request_SetResponseText(rq, "Ok"); + + GBAS(dbuf, "

Devices

\n"); + GBAS(dbuf, + "\n" + "\n" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "\n" + "\n" + "\n"); + + device=AQH_Device_List_First(deviceList); + while(device) { + const char *s; + + GBAA(dbuf, ""); + /* name for system */ + s=AQH_Device_GetNameForSystem(device); + GBAS(dbuf,"", s?s:""); + /* room */ + s=AQH_Device_GetRoomName(device); + GBAA(dbuf, "", s?s:""); + /* location */ + s=AQH_Device_GetLocation(device); + GBAA(dbuf, "", s?s:""); + /* description */ + s=AQH_Device_GetDescription(device); + GBAA(dbuf, "", s?s:""); + /* device type */ + s=AQH_Device_GetDeviceType(device); + GBAA(dbuf, "", s?s:""); + /* driver */ + s=AQH_Device_GetDriver(device); + GBAA(dbuf, "", s?s:""); + /* short device name */ + s=AQH_Device_GetName(device); + GBAA(dbuf, "", s?s:""); + /* GUI name for device */ + s=AQH_Device_GetNameForGui(device); + GBAA(dbuf, "", s?s:""); + /* manufacturer */ + s=AQH_Device_GetManufacturer(device); + GBAA(dbuf, "", s?s:""); + + GBAA(dbuf, ""); + device=AQH_Device_List_Next(device); } + + GBAS(dbuf, + "\n" + "
Name For SystemRoomLocationDescriptionTypeDriverNameGUI NameManufacturer
%s%s%s%s%s%s%s%s%s
\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) { - uint32_t perms; + GWEN_DB_NODE *dbQuery; + const char *sDeviceName; - perms=AQH_ModService_GetUserPerms(m); - DBG_ERROR(NULL, "Perms=%08x", perms); - if ((perms & AQH_MODDEVICES_PERMS_DEVICEREAD) && - (perms & AQH_MODDEVICES_PERMS_VALUEREAD)) { - 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, "

Values for Device %s

\n", sDeviceName); - GWEN_Buffer_AppendString(dbuf, - "\n" - "" - "" - "" - "" - "" - "" - "" - "" - "" - "\n" - "\n"); - - value=AQH_Value_List_First(valueList); - while(value) { - const char *s; - - GWEN_Buffer_AppendString(dbuf, ""); - - s=AQH_Value_GetName(value); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - - s=AQH_ValueType_toString(AQH_Value_GetValueType(value)); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - - s=AQH_ValueModality_toString(AQH_Value_GetModality(value)); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - - s=AQH_Value_GetDriver(value); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - - s=AQH_Value_GetDeviceNameForSystem(value); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - - s=AQH_Value_GetNameForSystem(value); - GWEN_Buffer_AppendArgs(dbuf, "", s?s:""); - - GWEN_Buffer_AppendArgs(dbuf, "\n"); - - value=AQH_Value_List_Next(value); - } - GWEN_Buffer_AppendString(dbuf, - "\n" - "
NameTypeModalityDriverDeviceName for System
%s%s%s%s%s%s
\n"); - AQH_Value_List_free(valueList); - } - } - AQCGI_Request_SetResponseCode(rq, 200); - AQCGI_Request_SetResponseText(rq, "Ok"); - } + dbQuery=AQCGI_Request_GetDbQuery(rq); + sDeviceName=GWEN_DB_GetCharValue(dbQuery, "device", 0, NULL); + if (!(sDeviceName && *sDeviceName)) + _runIndex(m, rq, session, dc, dbuf); else { - GWEN_Buffer_AppendString(dbuf, "

No permissions to read devices/values.

"); - AQCGI_Request_SetResponseCode(rq, 200); - AQCGI_Request_SetResponseText(rq, "Ok"); + AQH_VALUE_LIST *valueList; + + valueList=AQH_DataClient_GetValues(dc, sDeviceName, 0); + if (valueList) { + AQH_VALUE *value; + + GBAA(dbuf, "

Values for Device %s

\n", sDeviceName); + GBAS(dbuf, + "\n" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "\n" + "\n"); + + value=AQH_Value_List_First(valueList); + while(value) { + const char *s; + + GBAS(dbuf, ""); + + s=AQH_Value_GetName(value); + GBAA(dbuf, "", s?s:""); + + s=AQH_ValueType_toString(AQH_Value_GetValueType(value)); + GBAA(dbuf, "", s?s:""); + + s=AQH_ValueModality_toString(AQH_Value_GetModality(value)); + GBAA(dbuf, "", s?s:""); + + s=AQH_Value_GetDriver(value); + GBAA(dbuf, "", s?s:""); + + s=AQH_Value_GetDeviceNameForSystem(value); + GBAA(dbuf, "", s?s:""); + + s=AQH_Value_GetNameForSystem(value); + GBAA(dbuf, "", s?s:""); + + GBAA(dbuf, "\n"); + + value=AQH_Value_List_Next(value); + } + GBAS(dbuf, + "\n" + "
NameTypeModalityDriverDeviceName for System
%s%s%s%s%s%s
\n"); + AQH_Value_List_free(valueList); + } } }