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.

HttpService configuration.
 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
  1. Open a web browser and navigate to:

    http://localhost:HTTP_PORT

  2. Browse the application structure:

[-] ObjectBrowse (HttpObjectBrowser)
    [-] MassSpringApp (RealTimeApplication)
        [-] Functions (ReferenceContainer)
            [-] GAMWriter (IOGAM)
  1. Verify that signal values are updating.

  2. Open:

    http://localhost:HTTP_PORT/ObjectBrowse/MassSpringApp/Functions/GAMWriter?TextMode=0

    and confirm that JSON data is returned.

  3. Open:

    http://localhost:HTTP_PORT/?path=MassSpring-1.html

    to view the dashboard.

  4. Navigate to the HttpMessageInterface:

[-] ObjectBrowse
    [-] HttpMessageInterface (HttpMessageInterface)
  1. Click GOTO_CONSTANT_REF and observe the system settling at a constant reference.

  2. Click GOTO_SWITCH_OFF and 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.

  1. Edit ../Configurations/MassSpring/RTApp-MassSpring-54.cfg and add a new message to set the value 3 (an example for value 1 is already provided).

  2. 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
  1. 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
  1. Open the HttpMessageInterface in the browser:

[-] ObjectBrowse
    [-] HttpMessageInterface (HttpMessageInterface)
  1. Click the new message and verify that ReferencePosition changes to 3.

Solution

Add the new message:

Updated HttpMessageInterface configuration.
 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    }