Rework LuCI build system
[project/luci.git] / applications / luci-app-minidlna / luasrc / controller / minidlna.lua
1 --[[
2 LuCI - Lua Configuration Interface - miniDLNA support
3
4 Copyright 2012 Gabor Juhos <juhosg@openwrt.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 module("luci.controller.minidlna", package.seeall)
16
17 function index()
18         if not nixio.fs.access("/etc/config/minidlna") then
19                 return
20         end
21
22         local page
23
24         page = entry({"admin", "services", "minidlna"}, cbi("minidlna"), _("miniDLNA"))
25         page.dependent = true
26
27         entry({"admin", "services", "minidlna_status"}, call("minidlna_status"))
28 end
29
30 function minidlna_status()
31         local sys  = require "luci.sys"
32         local uci  = require "luci.model.uci".cursor()
33         local port = tonumber(uci:get_first("minidlna", "minidlna", "port"))
34
35         local status = {
36                 running = (sys.call("pidof minidlna >/dev/null") == 0),
37                 audio   = 0,
38                 video   = 0,
39                 image   = 0
40         }
41
42         if status.running then
43                 local fd = sys.httpget("http://127.0.0.1:%d/" % (port or 8200), true)
44                 if fd then
45                         local html = fd:read("*a")
46                         if html then
47                                 status.audio = (tonumber(html:match("Audio files</td><td>(%d+)")) or 0)
48                                 status.video = (tonumber(html:match("Video files</td><td>(%d+)")) or 0)
49                                 status.image = (tonumber(html:match("Image files</td><td>(%d+)")) or 0)
50                         end
51                         fd:close()
52                 end
53         end
54
55         luci.http.prepare_content("application/json")
56         luci.http.write_json(status)
57 end