Skip to main content

Storage

storage module implements an interface to a persistent key-value storage. Every Virtual UCMs has it's own storage, it is not shared with other Virtual UCMs running on the same Enapter Gateway.

storage.write

-- @param key string
-- @param value string
-- @return number
function storage.write(key, value) end

Writes the value to the persistent storage by key. Later the value can be accessed using storage.read by the same key.

Returns 0 if data was written successfully, otherwise returns error code (use storage.err_to_str to convert it to string representation).

Example

local result = storage.write("mykey", "myvalue")
if result ~= 0 then
enapter.log("Cannot write value: "..tostring(result).." "..storage.err_to_str(result), "error", true)
end

storage.read

-- @param key string
-- @return string|nil, number string
function storage.read(key) end

Reads the value from persistent storage by the given key.

Returns data as string or nil if the value is not set in the storage or in case of error. Error code is returned as a second return value (use storage.err_to_str to convert it to string representation).

Example

local value, result = storage.read("mykey")
if result == 0 then
enapter.log("Value: "..value)
else
enapter.log("Cannot write value: "..tostring(result).." "..storage.err_to_str(result), "error", true)
end

storage.remove

-- @param key string
---@return number
function storage.remove(key) end

Removes data from persistent storage.

Returns 0 if data was removed successfully, otherwise returns error code (use storage.err_to_str to convert it to string representation).

Example

local result = storage.remove("mykey")
if result ~= 0 then
enapter.log("Cannot remove value: "..tostring(result).." "..storage.err_to_str(result), "error", true)
end

storage.err_to_str

-- @param error number
-- @return string
function storage.err_to_str(error)
end

Returns string representation of storage function return code.

Hardware diversity is welcome. Integrate any device into a unified energy network.
© 2024 Enapter
Developer toolkit
DocumentationReference