Skip to main content

Storage

storage implements an interface to persistent flash memory.

Flash Cycles Limit

Flash memory has a limited number of writes which can be performed before the flash reaches the end of life. On Enapter UCMs it's about 60,000,000 writes of the 128 byte values (or 128 char strings).

If you want to store some data periodically and want your flash to work for at least 10 years, then the minimum interval for storage.write call is once per 5 seconds.

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.

key has length limit with 15 characters.

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

Example

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

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

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