Difference between revisions of "Static data load"

From Sim Innovations Wiki
Jump to navigation Jump to search
(24 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Description ==
== 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, 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 XML, JSON or CSV file.
'''static_data_load''' is used to get load static data from a JSON, CSV or text file.


== Return value ==  
== Return value ==  


This function won't return any value.
{| class="wikitable"
|-
! Argument !! Type !! Description
|-
| '''data''' || ''Object'' || The data in the static file. Will only return data when reading data synchronously.
|}


== Arguments ==
== Arguments ==
Line 15: Line 23:
! # !! Argument !! Type !! Description
! # !! Argument !! Type !! Description
|-
|-
| 1 || '''path''' || ''String'' || The location of the static file inside the resource folder (*.xml or *.json).
| 1 || '''path''' || ''String'' || The location of the static file inside the resource 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 ==
 
{| class="wikitable"
|-
! Name !! Type !! Default !! Description
|-
| '''csv_header''' || ''Boolean'' || true || Determines if the CSV has a header.
|-
|-
| 2 || '''callback''' || ''Function'' || Function is called when data has been loaded. Data argument contains the data, is nil on error.
|}
|}
== 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 (JSON) ==
Line 42: Line 71:
</source>
</source>


Lua code to read JSON example above
=== Synchronous ===
Lua code to read JSON example above synchronously.
 
<source lang="lua">
data = static_data_load("person.json")
 
-- 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
</source>
 
=== Asynchronous ===
Lua code to read JSON example above asynchronously.
 
<source lang="lua">
<source lang="lua">
static_data_load("person.json", function(data)
static_data_load("person.json", function(data)
Line 61: Line 110:


Example cars.csv file:
Example cars.csv file:
  Year,Make,Model,Description,Price
Year,Make,Model,Description,Price
  1997,Ford,E350,"ac, abs, moon",3000.00
1997,Ford,E350,"ac, abs, moon",$3000.00
  1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,Venture,"Extended Edition",$4900.00
  1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1999,Chevy,Venture,"Extended Edition, Very Large",$5000.00
  1996,Jeep,Grand Cherokee,"MUST SELL!
1996,Jeep,Grand Cherokee,"MUST SELL!",$2500.00
 
=== Synchronous ===
Lua code to read CSV example above synchronously.
 
<source lang="lua">
data = static_data_load("cars.csv", "csv_header=true")
-- 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
</source>
 
=== Asynchronous ===
Lua code to read CSV example above asynchronously.


Lua code to read CSV example above
<source lang="lua">
<source lang="lua">
static_data_load("cars.csv", function(data)
static_data_load("cars.csv", function(data)
   -- Print all cars
   -- Print all cars
   for key,value in pairs(data) do
   for key,value in pairs(data) do
     print(value["Make"] .. " " value["Model"] .. " from " .. value["Year"] .. ", now only for " .. value["Price"] .. "!")
     print(value["Make"] .. " " .. value["Model"] .. " " .. value["Description"] .. " from " .. value["Year"] .. ", now only for " .. value["Price"] .. "!")
  end
end)
</source>
 
== 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).
<source lang="lua">
data = static_data_load("cars.txt")
-- Print all cars
for key, value in pairs(data) do
  print("line " .. key .. "has car: " .. value)
end
</source>
 
=== 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).
<source lang="lua">
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
end)
end)
</source>
</source>

Revision as of 11:11, 7 March 2020

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.

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 resource 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.

data = static_data_load("person.json")

-- 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

Asynchronous

Lua code to read JSON example above asynchronously.

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.

data = static_data_load("cars.csv", "csv_header=true")
-- 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

Asynchronous

Lua code to read CSV example above asynchronously.

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).

data = static_data_load("cars.txt")
-- Print all cars
for key, value in pairs(data) do
  print("line " .. key .. "has car: " .. value)
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).

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)