Static data load
Description
data = static_data_load(path) data = static_data_load(path, options) (from AM/AP 3.6 or later) static_data_load(path, callback) static_data_load(path, options, callback) (from AM/AP 3.6 or later)
static_data_load is used to get load static data from a JSON, CSV or text file located in the resources folder.
Return value
Argument | Type | Description |
---|---|---|
data | Object | The data in the static file. Will only return data when reading data synchronously. |
Arguments
# | Argument | Type | Description |
---|---|---|---|
1 | path | String | The location of the static file inside the resources folder (*.csv, *.json or *.txt). |
2 | options | String | (Options) Extra options. See the table below for available options. |
3 | callback | Function | (Optional) Function is called when data has been loaded. Data argument contains the data, is nil on error. The file will be read synchronously when the callback function is not provided. |
Options
Name | Type | Default | Description |
---|---|---|---|
csv_header | Boolean | true | Determines if the CSV has a header. |
Synchronous vs Asynchronous
Reading a file takes some time, depending on the size of the file and the speed of your hard drive and your PC.
When you don't want your instrument or panel to have to wait for the file access, you can choose for asynchronous mode. The reading will happen in the background, and you will get a callback when the file has been read.
You can also read the file synchronous, which means that your instrument or panel will wait for the file to become ready. This is okay for the initial execution, but should be avoided when the instrument or panel is running.
Example (JSON)
Example person.json file:
{
"firstName": "John",
"lastName": "Smith",
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567",
"123 456-7890"
]
}
Synchronous
Lua code to read JSON example above synchronously.
-- person.json is location int he resources folder
data = static_data_load("person.json")
if data ~= nil then
-- Print first name
print(data["firstName"])
-- Print street address
print(data["address"]["streetAddress"])
-- Print all phone numbers
for key,value in pairs(data["phoneNumbers"]) do
print(value)
end
end
Asynchronous
Lua code to read JSON example above asynchronously.
-- person.json is location int he resources folder
static_data_load("person.json", function(data)
-- Print first name
print(data["firstName"])
-- Print street address
print(data["address"]["streetAddress"])
-- Print all phone numbers
for key,value in pairs(data["phoneNumbers"]) do
print(value)
end
end)
Example (CSV)
Example cars.csv file:
Year,Make,Model,Description,Price 1997,Ford,E350,"ac, abs, moon",$3000.00 1999,Chevy,Venture,"Extended Edition",$4900.00 1999,Chevy,Venture,"Extended Edition, Very Large",$5000.00 1996,Jeep,Grand Cherokee,"MUST SELL!",$2500.00
Synchronous
Lua code to read CSV example above synchronously.
-- cars.csv is location int he resources folder
data = static_data_load("cars.csv", "csv_header=true")
if data ~= nil then
-- Print all cars
for key,value in pairs(data) do
print(value["Make"] .. " " .. value["Model"] .. " " .. value["Description"] .. " from " .. value["Year"] .. ", now only for " .. value["Price"] .. "!")
end
end
Asynchronous
Lua code to read CSV example above asynchronously.
-- cars.csv is location int he resources folder
static_data_load("cars.csv", function(data)
-- Print all cars
for key,value in pairs(data) do
print(value["Make"] .. " " .. value["Model"] .. " " .. value["Description"] .. " from " .. value["Year"] .. ", now only for " .. value["Price"] .. "!")
end
end)
Example (Text)
Example cars.txt file:
Ford Chevy Jeep
Synchronous
Lua code to read Txt example above synchronously. Data will be split in lines. The new line character can be either \r\n (Windows) or \n (Unix).
-- cars.txt is location int he resources folder
data = static_data_load("cars.txt")
if data ~= nil then
-- Print all cars
for key, value in pairs(data) do
print("line " .. key .. "has car: " .. value)
end
end
Asynchronous
Lua code to read Txt example above asynchronously. Data will be split in lines. The new line character can be either \r\n (Windows) or \n (Unix).
-- cars.txt is location int he resources folder
static_data_load("cars.txt", function(data)
-- Print all cars
for key, value in pairs(data) do
print("line " .. key .. "has car: " .. value)
end
end)