tSIP: updating settings from Lua

"Script" type buttons with Lua UpdateSettings(json) function can be used to quickly change settings, e.g. switch between different SIP accounts.

tSIP.json is main settings file, containing almost everything - except for buttons configuration. I would recommend JSONedit for working with JSON, especially that same as tSIP it is non-Unicode application (though this would be more important for tSIP_buttons.json, as most settings in main file would probably be fine with base ASCII set).
tSIP settings JSON

Backup your tSIP.json file first. It might be also advisible to open/edit copy of tSIP.json rather than one used by running application.

JSONedit counts 340 nodes in default file from tSIP 0.2.05, but in typical case only few of them would need to be modified when button is pressed. Assuming that we need to change account settings, let's delete ([Del] key works in tree view when node is selected) everything but account settings.
tSIP settings JSON

Let's assume that we need to change only username, password and server address. Delete everything else.
tSIP settings JSON

Switch to text view and copy content.
tSIP settings JSON

Copy JSON text. Following script might be simple way of updating settings with inserted JSON ("[[" and "]]" are marking multiline string in Lua):

local settings = [[
{
   "Accounts" : [
      {
         "pwd" : "3001",
         "reg_server" : "192.168.0.12:5060",
         "user" : "3001"
      }
   ]
}
]]

UpdateSettings(settings)

Let's assume there would be switching between 3 accounts and buttons #15, #16, #17 (3 first button from default "console" part) would be used to switch. It might be easier to manage single script than 3 slightly different script copies:
tSIP settings JSON

To visually indicate which button was pressed (which account was activated) Lua SetButtonImage() function can be used - with empty.bmp and check.bmp included by default in tSIP archive.
tSIP settings JSON

Final script to assign to buttons #15, #16 and #17, switching between accounts 3001, 3002 and 3003 respectively:

local execSourceType = GetExecSourceType()
if execSourceType ~= 0 then
	ShowMessage("This script is supposed to be triggered by button")
	return
end

local btnId = GetExecSourceId()
local settings

if btnId == 15 then
	settings = [[
	{
	   "Accounts" : [
		  {
			 "pwd" : "3001",
			 "reg_server" : "192.168.0.12:5060",
			 "user" : "3001"
		  }
	   ]
	}
	]]
elseif btnId == 16 then
	settings = [[
	{
	   "Accounts" : [
		  {
			 "pwd" : "3002",
			 "reg_server" : "192.168.0.12:5060",
			 "user" : "3002"
		  }
	   ]
	}
	]]
elseif btnId == 17 then
	settings = [[
	{
	   "Accounts" : [
		  {
			 "pwd" : "3003",
			 "reg_server" : "192.168.0.12:5060",
			 "user" : "3003"
		  }
	   ]
	}
	]]
else
	ShowMessage("Unexpected button id!")
	return  
end

SetButtonImage(15, "empty.bmp")
SetButtonImage(16, "empty.bmp")
SetButtonImage(17, "empty.bmp")

SetButtonImage(btnId, "check.bmp")

UpdateSettings(settings)

To keep GUI state concise we might add extra "on startup" event script that would set proper button bitmap depending on current setting when starting application:
tSIP settings JSON

local user = GetUserName()

if user == "3001" then
	SetButtonImage(15, "check.bmp")
elseif user == "3002" then
	SetButtonImage(16, "check.bmp")
elseif user == "3003" then
	SetButtonImage(17, "check.bmp")
end

Note: many of the settings would cause user agent restart (disconnecting current call, re-registration, re-subscriptions) if changed.

Scripts from this page as files: update_settings_switch_account_scripts.zip.

Back to howto list.