phones.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local ast = require("luci.asterisk")
  4. cbimap = Map("asterisk", "Registered Phones")
  5. cbimap.pageaction = false
  6. local sip_peers = { }
  7. cbimap.uci:foreach("asterisk", "sip",
  8. function(s)
  9. if s.type ~= "peer" then
  10. s.name = s['.name']
  11. s.info = ast.sip.peer(s.name)
  12. sip_peers[s.name] = s
  13. end
  14. end)
  15. sip_table = cbimap:section(TypedSection, "sip", "SIP Phones")
  16. sip_table.template = "cbi/tblsection"
  17. sip_table.extedit = luci.dispatcher.build_url("admin", "asterisk", "phones", "sip", "%s")
  18. sip_table.addremove = true
  19. function sip_table.filter(self, s)
  20. return s and not cbimap.uci:get_bool("asterisk", s, "provider")
  21. end
  22. function sip_table.create(self, section)
  23. if TypedSection.create(self, section) then
  24. created = section
  25. cbimap.uci:tset("asterisk", section, {
  26. type = "friend",
  27. qualify = "yes",
  28. provider = "no",
  29. host = "dynamic",
  30. nat = "no",
  31. canreinvite = "no",
  32. extension = section:match("^%d+$") and section or "",
  33. username = section:match("^%d+$") and section or ""
  34. })
  35. else
  36. self.invalid_cts = true
  37. end
  38. end
  39. function sip_table.parse(self, ...)
  40. TypedSection.parse(self, ...)
  41. if created then
  42. cbimap.uci:save("asterisk")
  43. luci.http.redirect(luci.dispatcher.build_url(
  44. "admin", "asterisk", "phones", "sip", created
  45. ))
  46. end
  47. end
  48. user = sip_table:option(DummyValue, "username", "Username")
  49. function user.cfgvalue(self, s)
  50. return sip_peers[s] and sip_peers[s].callerid or
  51. AbstractValue.cfgvalue(self, s)
  52. end
  53. host = sip_table:option(DummyValue, "host", "Hostname")
  54. function host.cfgvalue(self, s)
  55. if sip_peers[s] and sip_peers[s].info.address then
  56. return "%s:%i" %{ sip_peers[s].info.address, sip_peers[s].info.port }
  57. else
  58. return "n/a"
  59. end
  60. end
  61. context = sip_table:option(DummyValue, "context", "Dialplan")
  62. context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
  63. online = sip_table:option(DummyValue, "online", "Registered")
  64. function online.cfgvalue(self, s)
  65. if sip_peers[s] and sip_peers[s].info.online == nil then
  66. return "n/a"
  67. else
  68. return sip_peers[s] and sip_peers[s].info.online
  69. and "yes" or "no (%s)" % {
  70. sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
  71. }
  72. end
  73. end
  74. delay = sip_table:option(DummyValue, "delay", "Delay")
  75. function delay.cfgvalue(self, s)
  76. if sip_peers[s] and sip_peers[s].info.online then
  77. return "%i ms" % sip_peers[s].info.delay
  78. else
  79. return "n/a"
  80. end
  81. end
  82. info = sip_table:option(Button, "_info", "Info")
  83. function info.write(self, s)
  84. luci.http.redirect(luci.dispatcher.build_url(
  85. "admin", "asterisk", "phones", "sip", s, "info"
  86. ))
  87. end
  88. return cbimap