2010-09-29 14:42:21 +00:00
|
|
|
--[[
|
|
|
|
LuCI - Lua Configuration Interface
|
|
|
|
|
|
|
|
Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
$Id: rpc.lua 5118 2009-07-23 03:32:30Z jow $
|
|
|
|
]]--
|
|
|
|
|
|
|
|
module ("luci.controller.restful", package.seeall)
|
|
|
|
|
|
|
|
function index()
|
|
|
|
local uci = require "luci.model.uci".cursor()
|
|
|
|
|
|
|
|
if uci:get("flukso", "main", "localEnable") == "1" then
|
|
|
|
uci:foreach("flukso", "sensor", function(section)
|
|
|
|
entry({section.id}, call("rest_sensor", {section.id}))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Decode a URL query string
|
|
|
|
-- @param QS the to-be-decoded query string
|
|
|
|
-- @return table containing [name] = value pairs
|
|
|
|
-- @see rest_sensor
|
|
|
|
local function decode(QS)
|
|
|
|
local param = {}
|
|
|
|
|
|
|
|
for name, value in QS:gmatch("([^&=]+)=([^&=]+)") do
|
|
|
|
param[name] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
return param
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Return a 400 error code to the client
|
|
|
|
-- @param message custom error message (optional)
|
|
|
|
-- @return false
|
|
|
|
function error400(message)
|
|
|
|
local http = require "luci.http"
|
|
|
|
|
|
|
|
http.status(400, "Bad Request")
|
|
|
|
message = message or "Bad Request"
|
|
|
|
|
|
|
|
http.prepare_content("text/plain")
|
|
|
|
http.write(message)
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Callback function for Flukso RESTful API
|
|
|
|
-- @param id requested sensor id (table)
|
|
|
|
-- @return json-encoded time series data
|
|
|
|
function rest_sensor(id)
|
|
|
|
local http = require "luci.http"
|
|
|
|
local ltn12 = require "luci.ltn12"
|
|
|
|
local uci = require "luci.model.uci".cursor()
|
|
|
|
|
2010-09-30 12:34:39 +00:00
|
|
|
local path = uci:get("flukso", "main", "localDir")
|
2010-10-01 15:14:50 +00:00
|
|
|
local version = uci:get("flukso", "main", "localVersion")
|
2010-09-29 14:42:21 +00:00
|
|
|
local param = decode(http.getenv("QUERY_STRING"))
|
|
|
|
|
2010-10-01 17:28:08 +00:00
|
|
|
http.prepare_content("application/json")
|
|
|
|
|
2010-10-01 15:14:50 +00:00
|
|
|
if param.interval == "minute" and param.unit == "watt" and param.version == version then
|
2010-09-29 14:42:21 +00:00
|
|
|
local source = ltn12.source.file(io.open(path .. "/" .. id[1], "r"))
|
2010-10-01 17:28:08 +00:00
|
|
|
|
|
|
|
if param.jsonp_callback then
|
|
|
|
source = ltn12.source.cat(ltn12.source.string(param.jsonp_callback .. "("), source, ltn12.source.string(")"))
|
|
|
|
end
|
|
|
|
|
2010-09-29 14:42:21 +00:00
|
|
|
ltn12.pump.all(source, http.write)
|
|
|
|
else
|
2010-10-01 17:28:08 +00:00
|
|
|
error400("Malformed query string: interval, unit and version query parameters are required.")
|
2010-09-29 14:42:21 +00:00
|
|
|
end
|
|
|
|
end
|