<< Click to Display Table of Contents >>
JSON Template |
JSON (JavaScript Object Notation) is a format for data exchange that is easily understood by humans, and also simple so that programs can perform information processing and extraction. The JSON format is build on top of a collection of pairs containing name and value and an ordered list of these pairs.
An Object is an unordered list of pairs of names and values. An object is delimited by braces, each name is defined between quotation marks followed by a colon and its respective value, and each pair is separated by commas.
An Array is an ordered list of values. An array is delimited by brackets and its values are separated by commas. Values can be numbers or texts, which must be represented between quotation marks. The next code contains an example of a message in JSON format.
{"s":1, "t":"2014-07-11T15:26:37Z", "q":192,"c":1, "x":-1.234,"y":0.234, "z":-0.234}
The next code contains a possible Template to extract the content of the previous message.
{"s":"DUMMY", "t":"TS_TEXT(%y-%m-%dT%H:%M:%SZ)","q":"QL_OPC", "c":"DUMMY", "x":"E3VAL","y":"E3VAL", "z":"E3VAL"}
In this case, users must create a Block Tag with three Block Elements, and each Block Element receives one of the x, y, and z values, respectively.
The next code contains an example of a message in JSON format.
{
"n_channels":2,
"timestamp":1504198675,
"hash":"1842E0F97392F08BDF996961A8333832AB06D113",
"battery":5.13,
"gmt":-3,
"tag_channels":["Analog1","Analog2"],
"value_channels":[28.100,27.200],
"tag_units":["°C","°C"],
"alarm_low":[0,0],
"alarm_high":[0,0],
"buzzer_state":0
}
The next code contains a possible Template for the previous example.
{
"n_channels":"DUMMY",
"timestamp":"TS_UNIX",
"hash":"DUMMY",
"battery":"V1",
"gmt":"V2",
"tag_channels":["V3","V4"],
"value_channels":["V5","V6"],
"tag_units":["V7","V8"],
"alarm_low":["V9","V10"],
"alarm_high":["V11","V12"],
"buzzer_state":"V13"
}
NOTE |
Both messages and Templates must be objects or arrays, therefore they must start with a brace ({) or bracket ([) character, respectively. |
Arrays can be used to receive a variable number of elements. The next codes show examples of messages.
Message 1 (one):
{
"MessageType": "Test",
"TagData": [
{
"Temperature": 11,
"Humidity": 50
},
{
"Temperature": 14,
"Humidity": 80
}
]
}
Message 2 (two):
{
"MessageType": "Test",
"TagData": [
{
"Temperature": 11,
"Humidity": 12
},
{
"Temperature": 14,
"Humidity": 66
}
{
"Temperature": 70,
"Humidity": 55
}
]
}
Inside an array, users can have 1 (one), 2 (two), 3 (three), or n sets of temperature and humidity in a single message, and it might be interesting if a "Test" Block Tag with two Elements, Temperature and Humidity, could receive a reading at each set found.
To do so, declare the Template with the Repeat_E3VAL keyword right after the bracket character that marks the array start, as in the next example.
{"MessageType":"DUMMY","TagData":[Repeat_E3VAL{"Temperature":"E3VAL","Humidity":"E3VAL"}]}
This way, when receiving message 2 (two), for example, there is a call to Block Tag's OnRead event for each set, according to the next example.
Sub [Sensor-001_OnRead]()
Application.Trace Item("Temperature").Value
Application.Trace Item("Humidity").Value
End Sub
Example of a log:
910 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 11 14
911 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 12 14
912 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 14 14
913 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 66 14
914 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 70 14
915 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 55 14
When declaring more E3VAL values before the repetition point, these values are repeated for each set. As an example, for a Template declared according to the next code:
{"MessageType":"E3VAL","TagData":[Repeat_E3VAL{"Temperature":"E3VAL","Humidity":"E3VAL"}]}
Users can have a Block Tag with 3 (three) Elements, type, temperature, and humidity, and the following values would be received at each OnRead event.
"Test";11;12 ' => First OnRead
"Test";14;66 ' => Second OnRead
"Test";70;55 ' => Third OnRead