Skip to content

Instantly share code, notes, and snippets.

@csrl
Forked from azdle/example_use.lua
Last active August 29, 2015 14:06
Show Gist options
  • Save csrl/6d8a8350a4ddaa4029b6 to your computer and use it in GitHub Desktop.
Save csrl/6d8a8350a4ddaa4029b6 to your computer and use it in GitHub Desktop.
local oneprpc = require 'oneprpc'
local json = require 'json'
local rpc = oneprpc:new({cik = "9829879a04efb8f4a1b42e22245739678fd4a94e"})
local status, rid = rpc.create(
"dataport",
{
format = "float",
name = "Temperature",
retention = {
count = "10",
duration = "infinity"
}
}
)
print(status, rid)
local status = rpc.map("alias", rid, "temp")
print(status)
local status, timestamp = rpc.write({alias="temp"}, 23.3)
print(status, timestamp)
local status, values = rpc.read({alias="temp"}, {})
print(status)
if "ok" == status then
print(json.encode(values))
end
local status = rpc.drop({alias="temp"})
print(status)
local status, info = rpc.info({alias=""}, {})
print(status)
if "ok" == status then
print(json.encode(info))
end
--[[
Copyright (c) 2014 Patrick Barrett
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]
local http = require('socket.http')
local json = require('json')
return {
_VERSION = "exosite-lua 0.1.0-dev",
_DESCRIPTION = "A Lua client library for the Exosite RPC API.",
_AUTHOR = "Patrick Barrett <patrick@mkii.org>",
_COPYRIGHT = "Copyright 2014 Patrick Barrett",
_LICENSE = "http://opensource.org/licenses/BSD-3-Clause",
url = 'https://m2.exosite.com/onep:v1/rpc/process',
cache = true,
new = function (self, settings)
local settings = {
url = nil ~= settings and settings.url or self.url,
cik = nil ~= settings and settings.cik or self.cik
}
return setmetatable({}, {
__index = function(rpc, procedure)
local f = function (...)
return self.process(settings, procedure, ...)
end
if self.cache then
rpc[procedure] = f -- cache generated function for reuse
end
return f
end
})
end,
process = function (settings, procedure, ...)
local url = settings.url
local cik = settings.cik
local call = {
id = 1,
procedure = procedure,
arguments = table.pack(...)
}
call.arguments.n = nil
local request = json.encode({
auth = {cik = cik},
calls = {call}
})
local reply = {}
local _, code, headers = http.request({
method = "POST",
url = url,
headers = {
["content-length"] = #request,
["content-type"] = "application/json"
},
source = ltn12.source.string(request),
sink = ltn12.sink.table(reply)
})
if code == 200 then
local response = json.decode(table.concat(reply))
if nil ~= response[1] then
if nil ~= response[1].status then
return response[1].status, response[1].result
end
return nil, response[1].error
end
return nil, response.error
end
return nil, {status = code, body = table.concat(reply), headers = headers}
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment