HttpService
The MARTe2 HttpService (see also HTTP) allows interaction with a MARTe2 application over HTTP.
It enables querying the internal state of any component using JSON. By default, GAMs and DataSources expose their signal values, which can be useful for building dashboards with external tools. Another useful feature is the ability to browse and navigate the application structure.
In this example, the HttpService is used to query the internal state of the application and to implement a simple HTML-based dashboard for the MassSpring system. The HttpMessageInterface component is used to send messages to the application, allowing the StateMachine to change state and thus modify the reference position.
Since the HttpService port must be unique to avoid conflicts, the configuration file ../Configurations/MassSpring/RTApp-MassSpring-53.cfg is automatically updated by Makefile.cfg.
1+WebRoot = {
2 Class = HttpObjectBrowser
3 Root = "."
4 +ObjectBrowse = {
5 Class = HttpObjectBrowser
6 Root = "/"
7 }
8 +ResourcesHtml = {
9 Class = HttpDirectoryResource
10 BaseDir = XSTR(MARTE2_HTTP_BASE_DIR)
11 }
12 +ResourcesHtmlTutorial = {
13 Class = HttpDirectoryResource
14 BaseDir = "../Resources/HTTP/"
15 }
16 +HttpMessageInterface = {
17 Class = HttpMessageInterface
18 +GOTO_SWITCH_OFF = {
19 Class = Message
20 Destination = StateMachine
21 Function = GOTO_SWITCH_OFF
22 Mode = ExpectsReply
23 }
24 +GOTO_CONSTANT_REF = {
25 Class = Message
26 Destination = StateMachine
27 Function = GOTO_CONSTANT_REF
28 Mode = ExpectsReply
29 }
30 +GOTO_WAVEFORM_REF = {
31 Class = Message
32 Destination = StateMachine
33 Function = GOTO_WAVEFORM_REF
34 Mode = ExpectsReply
35 }
36 }
37}
38+WebServer = {
39 Class = HttpService
40 Port = HTTP_PORT
41 WebRoot = WebRoot
42 Timeout = 0
43 ListenMaxConnections = 255
44 AcceptTimeout = 1000
45 MaxNumberOfThreads = 8
46 MinNumberOfThreads = 1
47}
Running the application
Start the application (note that HTTP_PORT is automatically assigned and printed):
make -C ../Configurations/MassSpring/ -f Makefile.cfg
HTTP_PORT=`awk '/\+WebServer/,/}/ {if ($1=="Port") print $3}' ../Configurations/MassSpring/RTApp-MassSpring-53_Gen.cfg`;echo "HTTP_PORT=$HTTP_PORT"
./MARTeApp.sh -f ../Configurations/MassSpring/RTApp-MassSpring-53_Gen.cfg -l RealTimeLoader -m StateMachine::START
Verify that the application is running correctly:
$ [Information - LoggerBroker.cpp:152]: Time [0:0]:990000
$ [Information - LoggerBroker.cpp:152]: ReferencePosition [0:0]:0.396000
Open a web browser and navigate to:
http://localhost:HTTP_PORTBrowse the application structure:
[-] ObjectBrowse (HttpObjectBrowser)
[-] MassSpringApp (RealTimeApplication)
[-] Functions (ReferenceContainer)
[-] GAMWriter (IOGAM)
Verify that signal values are updating.
Open:
http://localhost:HTTP_PORT/ObjectBrowse/MassSpringApp/Functions/GAMWriter?TextMode=0and confirm that JSON data is returned.
Open:
http://localhost:HTTP_PORT/?path=MassSpring-1.htmlto view the dashboard.
Navigate to the
HttpMessageInterface:
[-] ObjectBrowse
[-] HttpMessageInterface (HttpMessageInterface)
Click
GOTO_CONSTANT_REFand observe the system settling at a constant reference.Click
GOTO_SWITCH_OFFand observe the position decaying to zero.
Exercises
Ex. 1: Change the reference position via HttpMessageInterface
The objective is to add a message that directly sets a new reference value in the ConstantGAM GAMReference.
Edit
../Configurations/MassSpring/RTApp-MassSpring-54.cfgand add a new message to set the value3(an example for value1is already provided).Start the application:
make -C ../Configurations/MassSpring/ -f Makefile.cfg
HTTP_PORT=`awk '/\+WebServer/,/}/ {if ($1=="Port") print $3}' ../Configurations/MassSpring/RTApp-MassSpring-54_Gen.cfg`;echo "HTTP_PORT=$HTTP_PORT"
./MARTeApp.sh -f ../Configurations/MassSpring/RTApp-MassSpring-54_Gen.cfg -l RealTimeLoader -m StateMachine::START
Verify output:
$ [Warning - Threads.cpp:185]: Failed to change the thread priority (likely due to insufficient permissions)
$ [Information - StateMachine.cpp:340]: In state (INITIAL) triggered message (StartNextStateExecutionRTApp)
$ [Information - LoggerBroker.cpp:152]: Time [0:0]:990000
$ [Information - LoggerBroker.cpp:152]: ReferencePosition [0:0]:2.000000
Open the
HttpMessageInterfacein the browser:
[-] ObjectBrowse
[-] HttpMessageInterface (HttpMessageInterface)
Click the new message and verify that
ReferencePositionchanges to3.
Solution
Add the new message:
1 +HttpMessageInterface = {
2 Class = HttpMessageInterface
3 +GOTO_SWITCH_OFF = {
4 Class = Message
5 Destination = StateMachine
6 Function = GOTO_SWITCH_OFF
7 Mode = ExpectsReply
8 }
9 +GOTO_CONSTANT_REF = {
10 Class = Message
11 Destination = StateMachine
12 Function = GOTO_CONSTANT_REF
13 Mode = ExpectsReply
14 }
15 +GOTO_WAVEFORM_REF = {
16 Class = Message
17 Destination = StateMachine
18 Function = GOTO_WAVEFORM_REF
19 Mode = ExpectsReply
20 }
21 +CHANGE_CONSTANT_REF_1 = {
22 Class = Message
23 Destination = MassSpringApp.Functions.GAMReference
24 Function = "SetOutput"
25 Mode = ExpectsReply
26 +Parameters = {
27 Class = ConfigurationDatabase
28 SignalName = "ReferencePosition"
29 SignalValue = 1.0
30 }
31 }
32 +CHANGE_CONSTANT_REF_3 = {
33 Class = Message
34 Destination = MassSpringApp.Functions.GAMReference
35 Function = "SetOutput"
36 Mode = ExpectsReply
37 +Parameters = {
38 Class = ConfigurationDatabase
39 SignalName = "ReferencePosition"
40 SignalValue = 3.0
41 }
42 }
43 }