Auerswald PBX templates for D.Series running 1.2

templates.xml
<?xml version="1.0" encoding="utf-8"?>
<templates>
  <!-- Activate a predefined config in the PBX
    parameter: identity for PBX identification
    parameter: configuration_id
    -->
    <template prio="10" name="@string/config_activation_title">
      <keyConfiguration>
        <lua>
          <code>
  local valid = false
  local active = false
  local on = "##8*81"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..on..konfid.."#"..user.."@"..host
    if unescape(uri) == test_uri_on then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity == "" or konfid == "" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(on..konfid.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if not active and valid then
      sip.invite(on..konfid.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="konfid"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/configuration_id">
        <path>//param[@name="konfid"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- switch the call forwarding within the PBX on or off
    parameter: identity
    parameter: msn: which msn to forward
    parameter: target: call forwarding target
    -->
  <template prio="10" name="@string/call_forwarding_extern_unconditional_title">
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local prefix ="##96"
  local on = "*21*"
  local off = "##21#"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
    if (unescape(uri) == test_uri_on) and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity =="" or target=="" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(prefix..msn..off.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="target"/>
          <param name="msn"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/msn">
        <path>//param[@name="msn"]/value</path>
      </parameter>
      <parameter name="@string/target">
        <path>//param[@name="target"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- switch the call forwarding for busy within the PBX on or off
    parameter: identity
    parameter: msn: which msn to forward
    parameter: target: call forwarding target
    -->
  <template prio="10" name="@string/call_forwarding_extern_busy_title">                
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local prefix ="##96"
  local on = "*67*"
  local off = "##67#"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
    if unescape(uri) == test_uri_on and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity =="" or target=="" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(prefix..msn..off.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="target"/>
          <param name="msn"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/msn">
        <path>//param[@name="msn"]/value</path>
      </parameter>
      <parameter name="@string/target">
        <path>//param[@name="target"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- switch the call forwarding for not responding within the PBX on or off
    parameter: identity
    parameter: msn: which msn to forward
    parameter: target: call forwarding target
    -->
  <template prio="10" name="@string/call_forwarding_extern_no_response_title">
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local prefix ="##96"
  local on = "*61*"
  local off = "##61#"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
    if unescape(uri) == test_uri_on and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity =="" or target=="" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(prefix..msn..off.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="target"/>
          <param name="msn"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/msn">
        <path>//param[@name="msn"]/value</path>
      </parameter>
      <parameter name="@string/target">
        <path>//param[@name="target"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- switch the call forwarding within the PBX on or off
    parameter: identity
    parameter: target: call forwarding target
    -->
  <template prio="10" name="@string/call_forwarding_unconditional_title">
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local on = "*21*"
  local off = "#21#"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
    if unescape(uri) == test_uri_on and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity == "" or target== "" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(off.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(on..target.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="target"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/target">
        <path>//param[@name="target"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- switch the call forwarding for busy within the PBX on or off
    parameter: identity
    parameter: target: call forwarding target
    -->
  <template prio="10" name="@string/call_forwarding_busy_title">
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local on = "*67*"
  local off = "#67#"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
    if unescape(uri) == test_uri_on and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity =="" or target=="" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(off.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(on..target.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="target"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/target">
        <path>//param[@name="target"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- switch the call forwarding for not responding within the PBX on or off
    parameter: identity
    parameter: target: call forwarding target
    -->
  <template prio="10" name="@string/call_forwarding_no_response_title">
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local on = "*61*"
  local off = "#61#"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
    if unescape(uri) == test_uri_on and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity =="" or target=="" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(off.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(on..target.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="target"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/target">
        <path>//param[@name="target"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- toggles do not disturb in the PBX
    parameter: identity specifying the PBX
    -->
  <template prio="10" name="@string/do_not_disturb_title">
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local on = "##8*211"
  local off = "##8*210"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "red" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..on.."#"..user.."@"..host
    if unescape(uri) == test_uri_on and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity == "" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(on.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(off.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(on.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- toggle a group membership
    parameter: identity specifying the PBX
    parameter: group id
    -->
  <template prio="10" name="@string/group_title">
    <keyConfiguration>
      <lua>
        <code>
  local valid = false
  local active = false
  local on = "##8*401"
  local off = "##8*400"
  local userpath = "/identities/identity["..identity.."]/"
  local host = config.get(userpath .. "host")
  local user = config.get(userpath .. "username")
 
  local function updateLed()
    if valid then
      if active then
        key:setLed( "green" )
      else
        key:setLed( "off" )
      end
    else
      key:setLed( "red", true )
    end
  end
 
  local unescape = function(url)
    return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  end
 
  local function check_sub(data, uri, handle)
    local x = xml.eval(data)
    local test_uri_on = "sip:"..on..group.."#"..user.."@"..host
    if unescape(uri) == test_uri_on and x ~= nil then
      for _,dlg in ipairs(x) do
        state = dlg:find("state") 
        if state and state[1] == "trying" then 
          active = true
        elseif state and state[1] == "terminated" then
          active = false
        end
      end
    end
    updateLed()
  end
 
  local function check_valid()
    if identity == "" or group == "" then
      valid = false
    else
      valid = true
    end
  end
 
  local function sub()
    sip.subscribe(on..group.."#"..user,"0",identity,check_sub)
  end
 
  function onKeyUp()
    if active and valid then
      sip.invite(off..group.."#"..user,false,identity,true)
    elseif valid then
      sip.invite(on..group.."#"..user,false,identity,true)
    end
  end
  check_valid()
  updateLed()
  sub()
        </code>
        <params>
          <param name="identity"/>
          <param name="group"/>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/identity">
        <path>//param[@name="identity"]/value</path>
      </parameter>
      <parameter name="@string/group">
        <path>//param[@name="group"]/value</path>
      </parameter>
    </parameters>
  </template>
 
  <!-- trigger a relay in the PBX
    parameter: identity specifying the PBX
    parameter: relay id
    -->
  <template prio="10" name="Relais" icon="@drawable/relay">
    <keyConfiguration>
      <lua>
        <code>
                     local valid = false
                     local active = false
                     local on = "##8*26"
 
                     local function updateLed()
			            if valid then
                            if active then
					            key:setLed( "green", true )
                                time.sleep("2")
                                key:setLed( "off")
                            end
                        else
                            key:setLed( "red", true )
		                end
                     end
 
                    local function check_valid()
                        if identity == "" or relid == "" then
                           valid = false
                        else
                           valid = true
                        end
                    end
 
                    function onKeyUp()
                    if valid then
                        sip.invite(on..relid.."#",false,identity,true)
                        active = true
                        updateLed()
                        end
                     end
                    check_valid()
                    updateLed()
					</code>
                     <params>
                        <param name="identity"/>
                        <param name="relid"/>
                     </params>
                 </lua>
             </keyConfiguration>
    <parameters>
                <parameter name="@string/identity">
                     <path>//param[@name="identity"]/value</path>
                 </parameter>
                <parameter name="Relay Id">
                     <path>//param[@name="relid"]/value</path>
             </parameter>
        </parameters>
  </template>  
 
</templates>