From 9e8b7680b67b6d8d661965178d2ad2ee13aeb63d Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Thu, 3 Feb 2011 12:27:24 +0100 Subject: [PATCH] [restful] port local JSON/REST interface to v2 --- .../openwrt/package/flukso/luasrc/restful.lua | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 mote/v2/openwrt/package/flukso/luasrc/restful.lua diff --git a/mote/v2/openwrt/package/flukso/luasrc/restful.lua b/mote/v2/openwrt/package/flukso/luasrc/restful.lua new file mode 100755 index 0000000..aff6eca --- /dev/null +++ b/mote/v2/openwrt/package/flukso/luasrc/restful.lua @@ -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 + + 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 . + +]]-- + + +--- 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 = "/var/run/fluksod/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