/**************************************************************************** * 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 #endif #include "./mdevices.h" #include "aqhome-cgi/service/module.h" #include "aqhome-cgi/modules/mdataclient.h" #include #include #include /* ------------------------------------------------------------------------------------------------ * defs and enums * ------------------------------------------------------------------------------------------------ */ #define GBAS GWEN_Buffer_AppendString #define GBAA GWEN_Buffer_AppendArgs #define P_DEVICEREAD AQH_MODDEVICES_PERMS_DEVICEREAD #define P_VALUEREAD AQH_MODDEVICES_PERMS_VALUEREAD /* ------------------------------------------------------------------------------------------------ * forward declarations * ------------------------------------------------------------------------------------------------ */ static void _createPermDefList(AQH_MODULE *m); 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, P_DEVICEREAD, _handleRqIndexGet}, {"values.html", AQCGI_REQUEST_METHOD_GET, P_DEVICEREAD | P_VALUEREAD, _handleRqValuesGet}, {NULL, 0, 0, NULL} }; /* ------------------------------------------------------------------------------------------------ * 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); } int AQH_ModDevices_Create(AQH_SERVICE *sv) { AQH_MODULE *m; int rv; m=AQH_Module_new(); AQH_Module_SetName(m, "devices"); AQH_Module_SetDescr(m, "device module"); AQH_Module_SetGuestPerms(m, 0); _createPermDefList(m); _createRoleList(m); rv=AQH_Service_AddModule(sv, m); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); } AQH_Module_free(m); return rv; } void _createPermDefList(AQH_MODULE *m) { AQH_PERMDEF_LIST *permDefList; permDefList=AQH_PermDef_List_new(); AQH_ModService_AddPermDef(permDefList, "DeviceRead", 0x001, "Read and list devices"); AQH_ModService_AddPermDef(permDefList, "DeviceWrite", 0x002, "Modify devices"); AQH_ModService_AddPermDef(permDefList, "DeviceAdd", 0x004, "Add devices"); AQH_ModService_AddPermDef(permDefList, "DeviceDel", 0x008, "Remove devices"); AQH_ModService_AddPermDef(permDefList, "ValueRead", 0x010, "Read and list values"); AQH_ModService_AddPermDef(permDefList, "ValueWrite", 0x020, "Modify values"); AQH_ModService_AddPermDef(permDefList, "ValueAdd", 0x040, "Add values"); AQH_ModService_AddPermDef(permDefList, "ValueDel", 0x080, "Remove values"); AQH_ModService_AddPermDef(permDefList, "ValueSet", 0x100, "Set values"); AQH_Module_SetPermDefList(m, permDefList); } void _createRoleList(AQH_MODULE *m) { AQH_ROLE_LIST *roleList; int id=0; roleList=AQH_Role_List_new(); AQH_ModService_AddRole(roleList, id++, "Reader", AQH_MODDEVICES_PERMS_DEVICEREAD | AQH_MODDEVICES_PERMS_VALUEREAD, "Read devices and values"); AQH_ModService_AddRole(roleList, id++, "Writer", AQH_MODDEVICES_PERMS_DEVICEREAD | AQH_MODDEVICES_PERMS_DEVICEWRITE | AQH_MODDEVICES_PERMS_DEVICEADD | AQH_MODDEVICES_PERMS_DEVICEDEL | AQH_MODDEVICES_PERMS_VALUEREAD | AQH_MODDEVICES_PERMS_VALUEWRITE | AQH_MODDEVICES_PERMS_VALUEADD | AQH_MODDEVICES_PERMS_VALUEDEL | AQH_MODDEVICES_PERMS_VALUESET, "Read and write devices and values"); AQH_ModService_AddRole(roleList, id++, "Setter", AQH_MODDEVICES_PERMS_DEVICEREAD | AQH_MODDEVICES_PERMS_VALUEREAD | AQH_MODDEVICES_PERMS_VALUESET, "Set values"); AQH_Module_SetRoleList(m, roleList); } 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) { 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) { AQH_DEVICE_LIST *deviceList; AQH_DEVICE *device; uint32_t perms; perms=AQH_ModService_GetUserPerms(m); 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; } 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); if (perms & AQH_MODDEVICES_PERMS_VALUEREAD) { GBAS(dbuf,"", s?s:""); } else GBAA(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%s
\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; 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); } } }