question

Aditya Prakash avatar image
5 Likes"
Aditya Prakash asked Ben Wilson edited

Web Communication Within FlexSim

I'm looking for some kind of tutorial/example for web communication in FlexSim. I need to interact with an API for my model. I have been through the forum and user manual, the best answer I have found is using applicationcommand("sendhttprequest"...).

Although I can work with it, it would great if I can get to know all the possible ways/commands for web communication in FlexSim. Also I want to know how applicationcommand("sendhttprequest"...) works?

FlexSim 16.0.1
apihttpweb communicationsendhttprequest
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ben Wilson avatar image
0 Likes"
Ben Wilson answered Ben Wilson edited

Since version 20.1.0 FlexSim has included a built-in HTTP FlexScript API:

FlexScript Class - Http.Request (docs.flexsim.com)

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ben Wilson avatar image
5 Likes"
Ben Wilson answered Ben Wilson edited

If you are looking to have your model directly send HTTP requests to some server on the Internet, and listen for and react to the response, then the forum example you likely found is a good way to do it.

I have attached Phil's sample model from that forum post (with some slight modifications) to this post. This example model makes use of both the script window and tree view. Both of these feature are unavailable in FlexSim Express, so you'll need a license to get much out of this example.

With the example model open in FlexSim, you'll see a script window open at the bottom of the screen with an example usage of the sendhttprequest application command. Press the script window's green "run" button to run the code, and then check out the treenode MODEL:/Tools/result to see the server's response.

I have annotated Phil's original model with comments in the script window to help explain what is going on. Here is some documentation for sendhttprequest:

example function call:

  1. applicationcommand("sendhttprequest", verb, server, object, data, silent, result);

verb is a string, such as "POST" or "GET", that describes the sort of HTTP request you're making. Please see this article regarding http request methods.

server is a string, either an IP address or a fully qualified domain name (FQDN), referencing the server to communicate with. In the attached example model, this is "maps.googleapis.com".

object is a string that represents the rest of the URL after the server name, including a leading forward slash. For instance, in our example model we're querying Google's Maps API for the distance between Vancouver, British Columbia, Canada and San Fransisco, California, US. The full URL to make this query is maps.googleapis.com/maps/api/distancematrix/?origins=Vancouver+BC&destinations=San+Fransisco&sensor=false. In this address, maps.googleapis.com is the server string, as described above, and /maps/api/distancematrix/?origins=Vancouver+BC&destinations=San+Fransisco&sensor=false is the object.

data is a string of data that can be posted to the server, separate from the data passed as part of the URL. The attached example does not make use of this parameter, passing instead an empty string.

silent is a boolean flag (1 or 0) which indicates whether you want any errors to be printed to FlexSim's system console (Debug > System Console).

result is a FlexSim treenode with text data and is the location where the server's response will be written.


· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Frenk Gao avatar image
2 Likes"
Frenk Gao answered
@Aditya Prakash

Or May be you can use

callwebscriptmethod() in flexsim, and fireFlexsimEvent in web

in this way, flexsim can send any infomation to web, and js can process the info, may comnunicate the Server side(ajax), and the send call back to flexsim by fireFlexsimEvent.

Attached is an example to show how it works by me.

For more infomation, you can look to the 'Start Page' , 'Dashboard' and flexsim's js file.

Sorry for my poor English!

trytogetflexsiminfo-en-v75.fsm


5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ben Wilson avatar image
2 Likes"
Ben Wilson answered Ben Wilson edited

Attached is another example to show how you could use the return data from the server to drive your model in some fashion.

This example queries a site that gives random colors. It returns color values in hexadecimal format, and I actually need the color in decimal RGB values, so I use the first HTML returned by the server to make a 2nd HTTP request. From the 2nd response I parse RGB color values and apply them to my model.

Press the script window's green "run" button to see the model respond to the data received from the remote server.


5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.