Storage
辅助头文件:
#include "brookesia/service_helper/system/storage.hpp"辅助类:
esp_brookesia::service::helper::Storage
概述
brookesia_service_storage 是为 ESP-Brookesia 生态系统提供的命名空间键值存储服务,提供:
命名空间管理:基于命名空间的键值对存储,支持多个独立的存储空间。
数据类型支持:支持 布尔值、整数、字符串 三种基本数据类型。
持久化存储:数据通过底层存储后端持久化,断电后不丢失。
线程安全:可选基于 TaskScheduler 实现异步任务调度,保证线程安全。
灵活查询:支持列出命名空间中的所有键值对,或按需获取指定键的值。
功能特性
命名空间管理
默认命名空间:如果不指定命名空间,将使用默认命名空间
"storage"。多命名空间:可以创建多个命名空间来组织不同类型的数据。
命名空间隔离:不同命名空间的数据相互独立,互不影响。
支持的数据类型
布尔值(Bool):
true或false。整数(Int):32 位有符号整数。
字符串(String):UTF-8 编码的字符串。
备注
除了上述三种基本数据类型外,Storage Helper 提供的 esp_brookesia::service::helper::Storage::save_key_value() 和 esp_brookesia::service::helper::Storage::get_key_value() 模板助手函数还支持存储和获取任意类型的数据。
核心功能
列出键信息:列出命名空间中所有键的信息(包括键名、类型等)。
设置键值对:支持批量设置多个键值对。
获取键值对:支持获取指定键的值,或获取命名空间中的所有键值对。
删除键值对:支持删除指定键,或清空整个命名空间。
服务接口
函数
KVList
描述
List key-value entries in a Storage namespace.
执行要求
是否需要调度器: 需要
参数
Nspace类型:
String是否必填: 可选
默认值:
"storage"描述: Namespace to list (optional). Uses the default namespace when omitted.
返回值
类型:
Array描述: Example: [{"nspace":"storage","key":"key1","type":"String"},{"nspace":"storage","key":"key2","type":"Int"}]
Schema JSON
展开查看 JSON
{
"name": "KVList",
"description": "List key-value entries in a Storage namespace.",
"require_scheduler": true,
"default_timeout_ms": 1000,
"parameters": [
{
"name": "Nspace",
"description": "Namespace to list (optional). Uses the default namespace when omitted.",
"type": "String",
"required": false,
"default_value": "storage"
}
],
"return_value": {
"type": "Array",
"description": "Example: [{\"nspace\":\"storage\",\"key\":\"key1\",\"type\":\"String\"},{\"nspace\":\"storage\",\"key\":\"key2\",\"type\":\"Int\"}]"
}
}
CLI 命令
svc_call Storage KVList {"Nspace":null}
KVSet
描述
Set key-value pairs in a Storage namespace.
执行要求
是否需要调度器: 需要
参数
Nspace类型:
String是否必填: 可选
默认值:
"storage"描述: Namespace to write (optional). Uses the default namespace when omitted or empty.
KeyValuePairs类型:
Object是否必填: 必填
描述: Key-value pairs as a JSON object. Allowed value types: ["Bool","Int","String"]. Example: {"key1":"value1","key2":2,"key3":true}
Schema JSON
展开查看 JSON
{
"name": "KVSet",
"description": "Set key-value pairs in a Storage namespace.",
"require_scheduler": true,
"default_timeout_ms": 1000,
"parameters": [
{
"name": "Nspace",
"description": "Namespace to write (optional). Uses the default namespace when omitted or empty.",
"type": "String",
"required": false,
"default_value": "storage"
},
{
"name": "KeyValuePairs",
"description": "Key-value pairs as a JSON object. Allowed value types: [\"Bool\",\"Int\",\"String\"]. Example: {\"key1\":\"value1\",\"key2\":2,\"key3\":true}",
"type": "Object",
"required": true,
"default_value": null
}
],
"return_value": null
}
CLI 命令
svc_call Storage KVSet {"Nspace":null,"KeyValuePairs":null}
KVGet
描述
Get key-value pairs by keys from a Storage namespace.
执行要求
是否需要调度器: 需要
参数
Nspace类型:
String是否必填: 可选
默认值:
"storage"描述: Namespace to read (optional). Uses the default namespace when omitted.
Keys类型:
Array是否必填: 可选
默认值:
[]描述: Keys to read as JSON array<string> (optional). Returns all pairs when omitted. Example: ["key1","key2","key3"]
返回值
类型:
Object描述: Example: {"key1":"value1","key2":2,"key3":true}
Schema JSON
展开查看 JSON
{
"name": "KVGet",
"description": "Get key-value pairs by keys from a Storage namespace.",
"require_scheduler": true,
"default_timeout_ms": 1000,
"parameters": [
{
"name": "Nspace",
"description": "Namespace to read (optional). Uses the default namespace when omitted.",
"type": "String",
"required": false,
"default_value": "storage"
},
{
"name": "Keys",
"description": "Keys to read as JSON array<string> (optional). Returns all pairs when omitted. Example: [\"key1\",\"key2\",\"key3\"]",
"type": "Array",
"required": false,
"default_value": []
}
],
"return_value": {
"type": "Object",
"description": "Example: {\"key1\":\"value1\",\"key2\":2,\"key3\":true}"
}
}
CLI 命令
svc_call Storage KVGet {"Nspace":null,"Keys":null}
KVErase
描述
Erase key-value pairs from a Storage namespace.
执行要求
是否需要调度器: 需要
参数
Nspace类型:
String是否必填: 可选
默认值:
"storage"描述: Namespace to erase (optional). Uses the default namespace when omitted.
Keys类型:
Array是否必填: 可选
默认值:
[]描述: Keys to erase as JSON array<string> (optional). Erases all pairs when omitted or empty. Example: ["key1","key2","key3"]
Schema JSON
展开查看 JSON
{
"name": "KVErase",
"description": "Erase key-value pairs from a Storage namespace.",
"require_scheduler": true,
"default_timeout_ms": 1000,
"parameters": [
{
"name": "Nspace",
"description": "Namespace to erase (optional). Uses the default namespace when omitted.",
"type": "String",
"required": false,
"default_value": "storage"
},
{
"name": "Keys",
"description": "Keys to erase as JSON array<string> (optional). Erases all pairs when omitted or empty. Example: [\"key1\",\"key2\",\"key3\"]",
"type": "Array",
"required": false,
"default_value": []
}
],
"return_value": null
}
CLI 命令
svc_call Storage KVErase {"Nspace":null,"Keys":null}
GetFileSystems
描述
Get mounted storage file systems.
执行要求
是否需要调度器: 需要
参数
无。
返回值
类型:
Array描述: Example: [{"fs_type":"LittleFS","medium_type":"Flash","mount_point":"/littlefs","supports_directories":true},{"fs_type":"FATFS","medium_type":"SDCard","mount_point":"/sdcard","supports_directories":true}]
Schema JSON
展开查看 JSON
{
"name": "GetFileSystems",
"description": "Get mounted storage file systems.",
"require_scheduler": true,
"default_timeout_ms": null,
"parameters": [],
"return_value": {
"type": "Array",
"description": "Example: [{\"fs_type\":\"LittleFS\",\"medium_type\":\"Flash\",\"mount_point\":\"/littlefs\",\"supports_directories\":true},{\"fs_type\":\"FATFS\",\"medium_type\":\"SDCard\",\"mount_point\":\"/sdcard\",\"supports_directories\":true}]"
}
}
CLI 命令
svc_call Storage GetFileSystems
GetFileSystemCapacity
描述
Get one mounted storage file system capacity.
执行要求
是否需要调度器: 需要
参数
MountPoint类型:
String是否必填: 必填
描述: Mount point returned by GetFileSystems.
返回值
类型:
Object描述: Example: {"total_bytes":1048576,"used_bytes":262144,"free_bytes":786432}
Schema JSON
展开查看 JSON
{
"name": "GetFileSystemCapacity",
"description": "Get one mounted storage file system capacity.",
"require_scheduler": true,
"default_timeout_ms": null,
"parameters": [
{
"name": "MountPoint",
"description": "Mount point returned by GetFileSystems.",
"type": "String",
"required": true,
"default_value": null
}
],
"return_value": {
"type": "Object",
"description": "Example: {\"total_bytes\":1048576,\"used_bytes\":262144,\"free_bytes\":786432}"
}
}
CLI 命令
svc_call Storage GetFileSystemCapacity {"MountPoint":null}
MakeKVKey
描述
Generate a key that satisfies the active Storage KV backend limits.
执行要求
是否需要调度器: 不需要
参数
Parts类型:
Array是否必填: 必填
描述: Name parts as JSON array<string>. Empty parts are rejected.
Separator类型:
String是否必填: 可选
默认值:
"."描述: Separator inserted between name parts.
返回值
类型:
Object描述: Example: {"name":"h1abc","original_name":"bkl.display_lcd.On","hashed":true,"warning":"KV key exceeded backend limit and was replaced by a stable hash"}
Schema JSON
展开查看 JSON
{
"name": "MakeKVKey",
"description": "Generate a key that satisfies the active Storage KV backend limits.",
"require_scheduler": false,
"default_timeout_ms": null,
"parameters": [
{
"name": "Parts",
"description": "Name parts as JSON array<string>. Empty parts are rejected.",
"type": "Array",
"required": true,
"default_value": null
},
{
"name": "Separator",
"description": "Separator inserted between name parts.",
"type": "String",
"required": false,
"default_value": "."
}
],
"return_value": {
"type": "Object",
"description": "Example: {\"name\":\"h1abc\",\"original_name\":\"bkl.display_lcd.On\",\"hashed\":true,\"warning\":\"KV key exceeded backend limit and was replaced by a stable hash\"}"
}
}
CLI 命令
svc_call Storage MakeKVKey {"Parts":null,"Separator":null}
MakeKVNamespace
描述
Generate a namespace that satisfies the active Storage KV backend limits.
执行要求
是否需要调度器: 不需要
参数
Parts类型:
Array是否必填: 必填
描述: Namespace parts as JSON array<string>. Empty parts are rejected.
Separator类型:
String是否必填: 可选
默认值:
"."描述: Separator inserted between namespace parts.
返回值
类型:
Object描述: Example: {"name":"Display","original_name":"Display","hashed":false,"warning":""}
Schema JSON
展开查看 JSON
{
"name": "MakeKVNamespace",
"description": "Generate a namespace that satisfies the active Storage KV backend limits.",
"require_scheduler": false,
"default_timeout_ms": null,
"parameters": [
{
"name": "Parts",
"description": "Namespace parts as JSON array<string>. Empty parts are rejected.",
"type": "Array",
"required": true,
"default_value": null
},
{
"name": "Separator",
"description": "Separator inserted between namespace parts.",
"type": "String",
"required": false,
"default_value": "."
}
],
"return_value": {
"type": "Object",
"description": "Example: {\"name\":\"Display\",\"original_name\":\"Display\",\"hashed\":false,\"warning\":\"\"}"
}
}
CLI 命令
svc_call Storage MakeKVNamespace {"Parts":null,"Separator":null}
事件
无。