/**************************************************************************** * This file is part of the project Gwenhywfar. * Gwenhywfar (c) by 2023 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 "aqhome/data/storage_p.h" #include "aqhome/data/storage_readxml.h" #include "aqhome/data/storage_writexml.h" #include #include #include /* ------------------------------------------------------------------------------------------------ * forward declarations * ------------------------------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------------------------------ * implementations * ------------------------------------------------------------------------------------------------ */ AQH_STORAGE *AQH_Storage_new(void) { AQH_STORAGE *sto; GWEN_NEW_OBJECT(AQH_STORAGE, sto); sto->roomList=AQH_Room_List_new(); sto->deviceList=AQH_Device_List_new(); sto->mqttTopicList=AQH_MqttTopic_List_new(); sto->valueList=AQH_Value_List_new(); return sto; } void AQH_Storage_free(AQH_STORAGE *sto) { if (sto) { AQH_Value_List_free(sto->valueList); AQH_MqttTopic_List_free(sto->mqttTopicList); AQH_Device_List_free(sto->deviceList); AQH_Room_List_free(sto->roomList); free(sto->stateFile); GWEN_FREE_OBJECT(sto); } } const char *AQH_Storage_GetStateFile(const AQH_STORAGE *sto) { return sto?(sto->stateFile):NULL; } void AQH_Storage_SetStateFile(AQH_STORAGE *sto, const char *s) { if (sto) { free(sto->stateFile); sto->stateFile=s?strdup(s):NULL; } } void AQH_Storage_AddRoom(AQH_STORAGE *sto, AQH_ROOM *r) { if (sto && r) { uint64_t id; id=++(sto->lastRoomId); AQH_Room_SetId(r, id); AQH_Room_List_Add(r, sto->roomList); } } AQH_ROOM_LIST *AQH_Storage_GetRoomList(const AQH_STORAGE *sto) { return sto?sto->roomList:NULL; } AQH_ROOM *AQH_Storage_GetRoomById(const AQH_STORAGE *sto, uint64_t id) { return sto?AQH_Room_List_GetById(sto->roomList, id):NULL; } AQH_ROOM *AQH_Storage_GetRoomByName(const AQH_STORAGE *sto, const char *s) { return sto?AQH_Room_List_GetByName(sto->roomList, s):NULL; } void AQH_Storage_AddDevice(AQH_STORAGE *sto, AQH_DEVICE *dev) { if (sto && dev) { uint64_t id; id=++(sto->lastDeviceId); AQH_Device_SetId(dev, id); AQH_Device_List_Add(dev, sto->deviceList); } } AQH_DEVICE_LIST *AQH_Storage_GetDeviceList(const AQH_STORAGE *sto) { return sto?sto->deviceList:NULL; } AQH_DEVICE *AQH_Storage_GetDeviceById(const AQH_STORAGE *sto, uint64_t id) { return sto?AQH_Device_List_GetById(sto->deviceList, id):NULL; } AQH_DEVICE *AQH_Storage_GetDeviceByName(const AQH_STORAGE *sto, const char *s) { return sto?AQH_Device_List_GetByName(sto->deviceList, s):NULL; } void AQH_Storage_AddMqttTopic(AQH_STORAGE *sto, AQH_MQTT_TOPIC *t) { if (sto && t) { uint64_t id; id=++(sto->lastTopicId); AQH_MqttTopic_SetId(t, id); AQH_MqttTopic_List_Add(t, sto->mqttTopicList); } } AQH_MQTT_TOPIC_LIST *AQH_Storage_GetMqttTopicList(const AQH_STORAGE *sto) { return sto?sto->mqttTopicList:NULL; } AQH_MQTT_TOPIC *AQH_Storage_GetMqttTopicById(const AQH_STORAGE *sto, uint64_t id) { return sto?AQH_MqttTopic_List_GetById(sto->mqttTopicList, id):NULL; } AQH_MQTT_TOPIC *AQH_Storage_GetMqttTopicByTopic(const AQH_STORAGE *sto, const char *topic) { return sto?AQH_MqttTopic_List_GetByTopic(sto->mqttTopicList, topic):NULL; } void AQH_Storage_AddValue(AQH_STORAGE *sto, AQH_VALUE *value) { if (sto && value) { uint64_t id; id=++(sto->lastValueId); AQH_Value_SetId(value, id); AQH_Value_List_Add(value, sto->valueList); } } AQH_VALUE_LIST *AQH_Storage_GetValueList(const AQH_STORAGE *sto) { return sto?sto->valueList:NULL; } AQH_VALUE *AQH_Storage_GetValueById(const AQH_STORAGE *sto, uint64_t id) { return sto?AQH_Value_List_GetById(sto->valueList, id):NULL; } AQH_VALUE *AQH_Storage_GetValueByName(const AQH_STORAGE *sto, const char *s) { return sto?AQH_Value_List_GetByName(sto->valueList, s):NULL; } uint32_t AQH_Storage_GetRuntimeFlags(const AQH_STORAGE *sto) { return sto?sto->runtimeFlags:0; } void AQH_Storage_SetRuntimeFlags(AQH_STORAGE *sto, uint32_t flags) { if (sto) sto->runtimeFlags=flags; } void AQH_Storage_AddRuntimeFlags(AQH_STORAGE *sto, uint32_t flags) { if (sto) sto->runtimeFlags|=flags; } void AQH_Storage_SubRuntimeFlags(AQH_STORAGE *sto, uint32_t flags) { if (sto) sto->runtimeFlags&=~flags; } void AQH_Storage_HandleMqttPublish(AQH_STORAGE *sto, const char *topic, const char *value) { /* TODO */ } int AQH_Storage_Init(AQH_STORAGE *sto) { int rv; rv=GWEN_Directory_GetPath(sto->stateFile, GWEN_PATH_FLAGS_CHECKROOT | GWEN_PATH_FLAGS_PATHMUSTEXIST | GWEN_PATH_FLAGS_NAMEMUSTEXIST | GWEN_PATH_FLAGS_VARIABLE); if (rv==0) { rv=AQH_Storage_ReadStateFile(sto, sto->stateFile); if (rv<0) { DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } } else { DBG_WARN(AQH_LOGDOMAIN, "State file \"%s\" not available, will try to create it later (%d)", sto->stateFile, rv); } return 0; } int AQH_Storage_WriteState(AQH_STORAGE *sto) { int rv; rv=AQH_Storage_WriteStateFile(sto, sto->stateFile); if (rv<0) { DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } sto->runtimeFlags&=~AQH_STORAGE_RTFLAGS_MODIFIED; return 0; }