347 lines
10 KiB
C
347 lines
10 KiB
C
/****************************************************************************
|
|
* 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 "./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
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
#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, "<p>No devices.</p>");
|
|
return;
|
|
}
|
|
|
|
GBAS(dbuf, "<h1>Devices</h1>\n");
|
|
GBAS(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;
|
|
|
|
GBAA(dbuf, "<tr>");
|
|
/* name for system */
|
|
s=AQH_Device_GetNameForSystem(device);
|
|
if (perms & AQH_MODDEVICES_PERMS_VALUEREAD) {
|
|
GBAS(dbuf,"<td><a href=\"values.html?device=");
|
|
GWEN_Text_EscapeToBufferTolerant(s, dbuf);
|
|
GBAA(dbuf,"\">%s</a></td>", s?s:"");
|
|
}
|
|
else
|
|
GBAA(dbuf,"<td>%s</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);
|
|
}
|
|
|
|
|
|
|
|
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, "<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);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|