It uses JSON.lua to en/decode the actual file, so that special characters are handled correctly.
To do wish list -
- How to handle Get() for a new state - it has to be Set() first.
Is there a way to auto-close?
Load from built-in editor.
Code: Select all
commandArray = {}
package.path = '/home/pi/domoticz/scripts/lua/?.lua;' .. package.path
STATE = require("STATE")
...
value = STATE.Get(stateName)
...
STATE.Set(stateName, value)
...
STATE.Close()
return commandArray
Code: Select all
local State = {};
local opened
local write
local states
function Open()
if not JSON then
-- print("STATE: Loading JSON")
JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
end
file = io.open("state.txt", "r")
json_text = file:read()
states = JSON:decode(json_text)
-- print("STATE: opened")
opened = 1
return states
end
function State.Get(key)
if not opened then Open() end
return states[key]
end
function State.Set(key, value)
if not opened then Open() end
states[key] = value
write = 1
end
function State.Close()
if write then
json_text = JSON:encode(states)
file = io.open("state.txt", "w")
file:write(json_text)
file:close()
-- print("STATE: closed")
end
end
return State;