tSIP softphone: settings for Lua scripts

This is proposed template for storing script settings, that is parameters that are supposed to survive application restart. Settings are stored in json file in application directory. JSON parsing and generating is handling by dkjson module, placed in \scripts\inc subdirectory.

  -- settings.lua
  local M = {} -- public interface
  
  local json = require("scripts.inc.dkjson")
  local filename = "lua_settings.json"
  
  function M.get()
  	local cfg = {}
  
  	local param1, param1IsSet = GetVariable("Param1")
  	if param1IsSet == 1 then
  		param1 = tonumber(param1)
  	else
  		-- default
  		param1 = 0
  	end
  
  	cfg.param1 = param1;
  	
  	return cfg
  end
  
  function M.load()
  	local cfg = M.get()
  
  	local f = io.open(filename, "r")
  	if f == nil then
  		return cfg
  	end
  	local str = f:read("*all")
  	f.close()
  	
  	local obj, pos, err = json.decode (str, 1, nil)
  	if err then
  		print ("Error:", err)
  		return cfg
  	else
  		cfg = obj
  	end	
  	
  	if cfg.param1 ~= nil then
  		SetVariable("Param1", cfg.param1)
  	end
  	
  	return cfg
  end
  
  function M.store()
  	local cfg = M.get()
  	local str = json.encode (cfg, { indent = true })
  	local f = assert(io.open(filename, "w"))
  	f:write(str)
  	f.close()
  end
  
  return M
  -- end of settings.lua
  
  
  -- settings_store.lua
  SetVariable("Param1", 1234)
  
  local settings = require("scripts.settings")
  settings.store()
  -- end of settings_store.lua  
  
  
  -- settings_load.lua
  local settings = require("scripts.settings")
  local cfg = settings.load()
  
  local str = string.format("Found in file: cfg.param1 = %d", cfg.param1)
  ShowMessage(str)
  -- end of settings_load.lua    
  

Back to howto list


 "Cookie monsters": 7701808    Parse time: 0.000 s