Skip to content

Instantly share code, notes, and snippets.

@azdle
Last active August 29, 2015 14:06
Show Gist options
  • Save azdle/0a50a2d962a4c06355f0 to your computer and use it in GitHub Desktop.
Save azdle/0a50a2d962a4c06355f0 to your computer and use it in GitHub Desktop.
Basic Lua Library for Exosite's JSON RPC API
local exosite = require 'exosite'
local json = require 'dkjson'
local exo = exosite.rpc:new{cik = "9829879a04efb8f4a1b42e22245739678fd4a94e"}
local status, response = exo:create{
"dataport",
{
format = "float",
name = "Temperature",
retention = {
count = "infinity",
duration = "infinity"
}
}
}
print(status, json.encode(response))
local rid = response.result
local status, response = exo:map{"alias", rid, "temp"}
print(status, json.encode(response))
local status, response = exo:write{{alias="temp"}, 23.3}
print(status, json.encode(response))
local status, response = exo:read{{alias="temp"}, {}}
print(status, json.encode(response))
local status, response = exo:drop{{alias="temp"}}
print(status, json.encode(response))
local status, response = exo:info{{alias=""}, {}}
print(status, json.encode(response))
--[[
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 'dkjson'
local exosite = {
_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"
}
exosite.rpc = {
URL = 'https://m2.exosite.com/onep:v1/rpc/process'
}
local mt = {
__index = function(self, procedure)
return function(self, arguments)
local this_call = {
id = math.random(10000),
procedure = procedure,
arguments = arguments
}
local status, ret, err = self:call(this_call)
if status == false then
return false, ret, err
else
return true, ret
end
end
end
}
function exosite.rpc:call(call)
local request = json.encode({
auth = {cik = self.cik},
calls = {call}
})
local resp = {}
local ret,code,hrd = http.request{
method = "POST",
url = self.URL,
headers = {
["content-length"] = #request,
["content-type"] = "application/json"
},
source = ltn12.source.string(request),
sink = ltn12.sink.table(resp)
}
if code == 200 then
return true, json.decode(table.concat(resp))[1]
else
return false, code, table.concat(resp)
end
end
function exosite.rpc:new(tbl)
assert(type(tbl.cik) == "string" and #tbl.cik == 40)
local o = tbl
setmetatable(o, {__index = self})
return o
end
setmetatable(exosite.rpc, mt)
-- Need to ensure this gets seeded
math.randomseed( os.time() )
return exosite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment