openwrt: Switching from lucid to uhttpd. The restful.lua CGI script has been kept sober on purpose to boost performance. The RESTful API can now handle 10 calls/s.
This commit is contained in:
parent
23ad638752
commit
850cb3d307
5 changed files with 160 additions and 76 deletions
|
|
@ -33,11 +33,7 @@ endef
|
|||
|
||||
define Package/flukso/install
|
||||
$(INSTALL_DIR) $(1)/usr/share/lua/flukso/
|
||||
$(CP) $(PKG_BUILD_DIR)/flukso.lua $(1)/usr/share/lua/flukso/
|
||||
$(CP) $(PKG_BUILD_DIR)/data.lua $(1)/usr/share/lua/flukso/
|
||||
$(CP) $(PKG_BUILD_DIR)/dbg.lua $(1)/usr/share/lua/flukso/
|
||||
$(CP) $(PKG_BUILD_DIR)/auth.lua $(1)/usr/share/lua/flukso/
|
||||
$(CP) $(PKG_BUILD_DIR)/heartbeat.lua $(1)/usr/share/lua/flukso/
|
||||
$(CP) $(PKG_BUILD_DIR)/{flukso,data,dbg,auth,heartbeat,restful}.lua $(1)/usr/share/lua/flukso/
|
||||
$(INSTALL_DIR) $(1)/etc/init.d/
|
||||
$(CP) $(PKG_BUILD_DIR)/flukso.init $(1)/etc/init.d/flukso
|
||||
$(INSTALL_DIR) $(1)/etc/config/
|
||||
|
|
|
|||
68
mote/v1/openwrt/package/flukso/src/restful.lua
Executable file
68
mote/v1/openwrt/package/flukso/src/restful.lua
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
--[[
|
||||
|
||||
restful.lua - CGI script providing a local RESTful API on the Fluksometer.
|
||||
|
||||
Copyright (c) 2010 Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
]]--
|
||||
|
||||
|
||||
--- Decode a URL's query string
|
||||
-- @param QS the to-be-decoded query string (optional)
|
||||
-- @return table containing [name] = value pairs
|
||||
local function query_decode(QS)
|
||||
local param = {}
|
||||
|
||||
for name, value in (QS or os.getenv("QUERY_STRING")):gmatch("([^&=]+)=([^&=]+)") do
|
||||
param[name] = value
|
||||
end
|
||||
|
||||
return param
|
||||
end
|
||||
|
||||
--- Fetch the sensor id in the HTTP request.
|
||||
-- @return sensor id
|
||||
local function sensor_id()
|
||||
return os.getenv("SCRIPT_NAME"):match("/sensor/([%x]+)")
|
||||
end
|
||||
|
||||
|
||||
local param = query_decode()
|
||||
local path = "/tmp/sensor/"
|
||||
local version = "1.0"
|
||||
|
||||
-- Hardcoding path and version parameters lowers GET response time from 200ms to 90ms.
|
||||
-- local uci = require "luci.model.uci".cursor()
|
||||
-- local path = uci:get("flukso", "main", "localDir") .. "/"
|
||||
-- local version = uci:get("flukso", "main", "localVersion")
|
||||
|
||||
if param.interval == "minute" and param.unit == "watt" and param.version == version then
|
||||
local pre, post = "", ""
|
||||
|
||||
if param.jsonp_callback then
|
||||
pre, post = param.jsonp_callback .. "(", ")"
|
||||
end
|
||||
|
||||
io.input(path .. sensor_id())
|
||||
|
||||
io.write("Content-Type: application/json", "\n\n")
|
||||
io.write(pre, io.read("*all"), post)
|
||||
else
|
||||
io.write("status: 400 Bad Request", "\n\n")
|
||||
io.write("Malformed query string: interval, unit and version query parameters are required.")
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue