avalon.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2014 Mikeqin <Fengling.Qin@gmail.com>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. $Id: init.lua 6731 2011-01-14 19:44:03Z soma $
  9. ]]--
  10. module("luci.controller.avalon", package.seeall)
  11. function index()
  12. local page = node("avalon")
  13. page.target = firstchild()
  14. page.title = _("Avalon")
  15. page.order = 8
  16. page.sysauth = "root"
  17. page.sysauth_authenticator = "avalonauth"
  18. entry({"avalon"}, alias("avalon", "page", "index"), nil, 90).dependent=false
  19. entry({"avalon", "page", "index"}, template("page/index"), _("Dashboard"))
  20. entry({"avalon", "page", "passwdchange"}, cbi("passwdchange"))
  21. entry({"avalon", "page", "network"}, cbi("network"), _("Network"))
  22. entry({"avalon", "page", "configure"}, cbi("cgsetting"), _("Configuration"))
  23. entry({"avalon", "api", "getstatus"}, call("api_getstatus"), nil)
  24. entry({"avalon", "api", "getlog"}, call("api_getlog"), nil)
  25. entry({"avalon", "api", "logout"}, call("action_logout"), _("Logout"))
  26. end
  27. function api_getstatus()
  28. local status = {
  29. elapsed = nil,
  30. ghsav = nil,
  31. ghsmm = nil,
  32. temp = nil,
  33. fan = nil,
  34. voltage = nil,
  35. modularcnt = 0,
  36. network = {},
  37. pool = {},
  38. openwrtver = nil,
  39. systime = nil,
  40. }
  41. -- Hashrate
  42. local summary = luci.util.execi("/usr/bin/cgminer-api -o summary | sed \"s/|/\\n/g\" ")
  43. if summary then
  44. for line in summary do
  45. local elapsed, ghsav = line:match(".*," ..
  46. "Elapsed=(-?[%d]+)," ..
  47. "MHS av=(-?[%d%.]+),")
  48. if ghsav then
  49. status.elapsed = tonumber(elapsed)
  50. status.ghsav = ghsav and (ghsav / 1000) or 0
  51. end
  52. end
  53. end
  54. -- Modulars information
  55. local stats = luci.util.execi("/usr/bin/cgminer-api -o estats | sed \"s/|/\\n/g\" | grep AV4 ")
  56. local devdata = {}
  57. if stats then
  58. for line in stats do
  59. local id = line:match(".*," ..
  60. "ID=AV4([%d]+),")
  61. if id then
  62. local istart, iend = line:find("MM ID")
  63. while (istart) do
  64. local istr = line:sub(istart)
  65. local index, temp, temp0, temp1, fan, v, ghsmm =
  66. istr:match("MM ID(%d+)=" ..
  67. ".-" ..
  68. "Temp%[(-?%d+)%]" ..
  69. ".-" ..
  70. "Temp0%[(-?%d+)%]" ..
  71. ".-" ..
  72. "Temp1%[(-?%d+)%]" ..
  73. ".-" ..
  74. "Fan%[(-?%d+)%]" ..
  75. ".-" ..
  76. "Vol%[(-?[%.%d]+)%]" ..
  77. ".-" ..
  78. "GHSmm%[(-?[%.%d]+)%]")
  79. devdata[#devdata+1] = {
  80. temp = tonumber(temp),
  81. temp0 = tonumber(temp0),
  82. temp1 = tonumber(temp1),
  83. fan = tonumber(fan),
  84. v = tonumber(v),
  85. ghsmm = tonumber(ghsmm)
  86. }
  87. istart, iend = line:find("MM ID", iend + 1)
  88. end
  89. end
  90. end
  91. end
  92. local modularcnt = table.getn(devdata)
  93. if modularcnt ~= 0 then
  94. local temp, fan, v, ghsmm = 0, 0, 0, 0;
  95. status.modularcnt = modularcnt
  96. for i, item in ipairs(devdata) do
  97. if temp < tonumber(item.temp) then
  98. temp = tonumber(item.temp)
  99. end
  100. if temp < tonumber(item.temp0) then
  101. temp = tonumber(item.temp0)
  102. end
  103. if temp < tonumber(item.temp1) then
  104. temp = tonumber(item.temp1)
  105. end
  106. if fan < tonumber(item.fan) then
  107. fan = tonumber(item.fan)
  108. end
  109. if i == 1 then
  110. v = tonumber(item.v)
  111. end
  112. if v > tonumber(item.v) then
  113. v = tonumber(item.v)
  114. end
  115. ghsmm = ghsmm + item.ghsmm
  116. end
  117. status.temp = tonumber(temp)
  118. status.ghsmm = tonumber(ghsmm)
  119. status.fan = tonumber(fan)
  120. status.voltage = tonumber(v)
  121. end
  122. -- pool info
  123. local data = {}
  124. local pools = luci.util.execi("/usr/bin/cgminer-api -o pools | sed \"s/|/\\n/g\" ")
  125. if pools then
  126. for line in pools do
  127. local pool, url, user, diff, accept =
  128. line:match("POOL=(-?%d+)," ..
  129. "URL=(.-)," ..
  130. "Status=Alive.-" ..
  131. "User=(.-)," ..
  132. ".-" ..
  133. "Diff1 Shares=(-?%d+)," ..
  134. ".-," ..
  135. "Difficulty Accepted=(-?%d+)[%.%d]+," ..
  136. ".-" ..
  137. "Stratum Active=true")
  138. if pool then
  139. status.pool[#status.pool+1] = {
  140. ['pool'] = pool,
  141. ['url'] = url,
  142. ['user'] = user,
  143. ['diff'] = tonumber(diff),
  144. ['accept'] = tonumber(accept)
  145. }
  146. end
  147. end
  148. end
  149. -- network info
  150. if nixio.fs.access("/bin/ubus") then
  151. local netm = require "luci.model.network".init()
  152. local net = netm:get_network("lan")
  153. local device = net and net:get_interface()
  154. if device then
  155. status.network['mac'] = device:mac()
  156. local _, a
  157. for _, a in ipairs(device:ipaddrs()) do
  158. status.network['ip4'] = a:host():string()
  159. end
  160. for _, a in ipairs(device:ip6addrs()) do
  161. if not a:is6linklocal() then
  162. status.network['ip6'] = a:host():string()
  163. end
  164. end
  165. end
  166. end
  167. local releasedate = io.popen("cat /etc/avalon_version")
  168. status.openwrtver = releasedate:read("*line")
  169. releasedate:close()
  170. status.systime = os.date("%c")
  171. luci.http.prepare_content("application/json")
  172. luci.http.write_json(status)
  173. end
  174. function api_getlog()
  175. local msg = {}
  176. local pp = io.popen("echo -n \"[Firmware Version] => \"; cat /etc/avalon_version; /usr/bin/cgminer-api stats;")
  177. msg.log = pp:read("*a")
  178. pp:close()
  179. luci.http.prepare_content("application/json")
  180. luci.http.write_json(msg)
  181. end
  182. function action_logout()
  183. local dsp = require "luci.dispatcher"
  184. local sauth = require "luci.sauth"
  185. if dsp.context.authsession then
  186. sauth.kill(dsp.context.authsession)
  187. dsp.context.urltoken.stok = nil
  188. end
  189. luci.http.header("Set-Cookie", "sysauth=; path=" .. dsp.build_url())
  190. luci.http.redirect(luci.dispatcher.build_url("avalon", "page", "index"))
  191. end