Merge pull request #956 from lucize/master
[project/luci.git] / applications / luci-app-minidlna / luasrc / controller / minidlna.lua
1 -- Copyright 2012 Gabor Juhos <juhosg@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.minidlna", package.seeall)
5
6 function index()
7         if not nixio.fs.access("/etc/config/minidlna") then
8                 return
9         end
10
11         local page
12
13         page = entry({"admin", "services", "minidlna"}, cbi("minidlna"), _("miniDLNA"))
14         page.dependent = true
15
16         entry({"admin", "services", "minidlna_status"}, call("minidlna_status"))
17 end
18
19 function minidlna_status()
20         local sys  = require "luci.sys"
21         local uci  = require "luci.model.uci".cursor()
22         local port = tonumber(uci:get_first("minidlna", "minidlna", "port"))
23
24         local status = {
25                 running = (sys.call("pidof minidlna >/dev/null") == 0),
26                 audio   = 0,
27                 video   = 0,
28                 image   = 0
29         }
30
31         if status.running then
32                 local fd = sys.httpget("http://127.0.0.1:%d/" % (port or 8200), true)
33                 if fd then
34                         local html = fd:read("*a")
35                         if html then
36                                 status.audio = (tonumber(html:match("Audio files</td><td>(%d+)")) or 0)
37                                 status.video = (tonumber(html:match("Video files</td><td>(%d+)")) or 0)
38                                 status.image = (tonumber(html:match("Image files</td><td>(%d+)")) or 0)
39                         end
40                         fd:close()
41                 end
42         end
43
44         luci.http.prepare_content("application/json")
45         luci.http.write_json(status)
46 end