Communicating between a Storyboard application and external sources is straightforward and flexible. Storyboard provides a number of connectivity options ranging from its native SBIO API for system engineers, to JSON (JavaScript Object Notation) for more web-centric programmers.
JSON is a great option to use with Storyboard because of how closely it resembles Lua. They are both lightweight for use in embedded applications, simple for humans to read and write, and easy for machines to parse and generate.
Using the Lua JSON module makes it easy to encode/decode incoming/outgoing JSON strings to Lua tables, which can then be parsed and used within the Storyboard application.
The following Lua example demonstrates how an incoming JSON string is decoded, and how the “message” data is used as the alarm text and the “timeout” data is used to dismiss the alarm dialog box after a certain amount of time has passed.
local json = require "dkjson"
function cb_alarm_show(mapargs)
local ev = mapargs.context_event_data
local json_data = ev["json"]
local data = {}
local json_table = json.decode(json_data)
if (json_table == nil) then
return nil
end
local alarm = json_table
-- text message
data["alarm_l.alarm_c.txt"] = alarm["message"]
gre.set_data(data)
-- make sure the layer is visible
data = {}
data["hidden"] = 0
gre.set_layer_attrs_global("alarm_l", data)
-- if timout isn't 0 set up a timeout callback
if alarm["timeout"] ~= 0 then
gre.timer_set_timeout(cb_alarm_hide, alarm["timeout"])
end
end
Not only can JSON be a solution for local system communication, but also for transmitting data remotely over the Internet. Simply adding the Lua Socket and Lua JSON modules enables Storyboard applications to communicate over the Internet, enabling engineers to easily create IoT (Internet of Things) applications.
Below is a an example of a Storyboard application requesting weather data from the Internet.
http = require("socket.http")
require('json')
function cb_get_weather(mapargs)
local v, b, c, h
local City = "Ottawa,CA"
local url_current
string.format("http://api.openweathermap.org/data/2.5/weather?q=%s&units=metric",City)
local data = {}
b, c, h = http.request(url_current)
v = json.decode(b)
data["Layer.temp.text"] = math.floor(v.main.temp)
gre.set_data(data)
end
These examples quickly highlight JSON communication. Another option is SBIO, which we will cover in another blog post. Stay tuned!