Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » 4DIAC - Framework for Distributed Industrial Automation and Control » json com layer(top level layer for putting data into JSON formate)
json com layer [message #1754454] Fri, 17 February 2017 22:57 Go to next message
Ernst Murnleitner is currently offline Ernst MurnleitnerFriend
Messages: 19
Registered: December 2016
Junior Member
I attach the source code for a new top level communication layer which we use on top of the mqtt layer.

Additional to the more complicated example which is described below,
json[1:l1/value,2:l1/intval,3:l1/l2/intval2:any].mqtt[tcp://127.0.0.1:1883,1,%topic%]
it can also be used more simple:
If ...
json[value].mqtt[tcp://127.0.0.1:1883,1,/topic/subtopic]
... is used as ID of one of the PUBLISH FB, ...
this mqtt message will be published:
topic: /topic/subtopic
payload: {"value":XXX}
... where XXX is the value which is connected to the SD_1 of PUBLISH1 (or ...2 etc)

jsoncomlayer-readme.txt:

== Introduction ==

jsoncomlayer adds a top level layer to FORTE.
It receives a string in JSON formate, extracts one or more values and transfers the values to the function block (e.g. SUBSCRIBE).
It reads values from a function block (e.g. PUBLISH) and writes them into a JSON-Object, which is transferred to the bottom layer.

It was tested using mqtt as the bottom layer.

== Files ==

in directory src/core/cominfra:

Implementation:
jsoncomlayer.cpp
jsoncomlayer.h

Helper functions for json parsing:
jsoncomlayer-readme.txt
json_spirit_error_position.h
json_spirit.h
json_spirit_reader.h
json_spirit_reader_implementation.h
json_spirit_reader_template.h
json_spirit_stream_reader.h
json_spirit_utils.h
json_spirit_value.h
json_spirit_value_implementation.h
json_spirit_writer.h
json_spirit_writer_implementation.h
json_spirit_writer_options.h
json_spirit_writer_template.h

add to CMakeLists.txt:
forte_add_network_layer(JSON ON "json" CJsonComLayer jsoncomlayer "Enable Forte json communication")


add -DFORTE_RTTI_AND_EXCEPTIONS=ON to the call of cmake, e.g. in setup_posix.sh

== Usage ==
Use the Publish/Subscribe function blocks of 4DIAC and configure it's ID data input as follows:

json[Index:(structure/)Variable(:type), Index2:(structure2/)Variable2(:type), ...].mqtt[tcp://ip:port, cientID, topic]

Whitespaces can be ommitted.

Index is the number of the input/output variable of the Funktionblock, beginning with 1.
The structure is optional. The levels of nested objects are seperated by "/".
The variable is the name of the value.
The optional type forces the data into a certain formate.

type can be one of: auto, number, real, int, bool, null, any.
The type influences, how the conversion between 4DIAC IEC type and JSON variable type is made.

auto: automatic conversion between 4DIAC IEC type and JSON variable type (e.g. json int fits to all int types, json bool fits to bool type etc.)
number: converts numbers (bool, int, real) but not string. The smallest possible representation is chosen (e.g. a IEC bool is representated by a JSON bool (values: true, false)
string: converts values to string
real: converts any number to a real (number with decimal point)
int: converts any number into an int (number without decimal point)
bool: converts any number into a bool (only the two states true or false)
any: like auto, but a string fits to a number and a number fits to string also



== Example ==

ID string for the Function block:
json[1:l1/value,2:l1/intval,3:l1/l2/intval2:any].mqtt[tcp://127.0.0.1:1883,1,%topic%]

A JSON object which fits:
{"l1":{"value":"111","intval":234,"l2":{"intval2":"45"}}}

Values assigned from JSON to the Function block:
1: the value "111" is a JSON string type (because the enclosing quotes). It can only be assigned to a string.
2: the value 234 is a JSON int type (because no decimal point and no quotes). It can only be assinged to one of the IEC int types.
3: the value "45" is a JSON string. It can be assignd to any IEC type (bool, integer, real, string), because the type "any" is used in the ID string.


(c) Ernst Murnleitner, Awite Bionergie GmbH 2016
Re: json com layer [message #1754485 is a reply to message #1754454] Sat, 18 February 2017 16:20 Go to previous message
Alois Zoitl is currently offline Alois ZoitlFriend
Messages: 1582
Registered: January 2014
Senior Member

Hi Ernst,

thanks a lot for your contribution. Having a json layer is definitely something which is extremely useful for 4diac users. Not only on for MQTT but also for other communication protocols.

Could you please be so kind and have a look in 4diac's documentation under contributing to FORTE because there are certain rules and procedures we have to follow on order that we can add your code to FORTE.

Thanks again,
Alois
Previous Topic:FORTE: To many files in modules
Next Topic:FORTE: Bugs in CLocalComLayer
Goto Forum:
  


Current Time: Fri Apr 19 20:27:25 GMT 2024

Powered by FUDForum. Page generated in 0.02916 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top