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
httpapiweb 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
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:

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.

Ben Wilson avatar image Ben Wilson ♦♦ commented ·

Regarding the data parameter - here is some additional information about how data should be formatted when passing to the server.

FlexSim escapes the data string. When the receiving server receives the request with that data, it is expecting the & symbol to determine different key-value pairs, but FlexSim is changing the & symbols into %26 characters and the server thinks the entire string is one key-value pair.

FlexSim only escapes characters in the data string up to the first # or ? character (including those characters). So everything after the ? is not escaped (which is what we want), the & symbol is actually read as a & symbol in the string (not just %26), and the server knows how to break up the different key-value pairs.

So the workaround to pass multiple key-value pairs in the data parameter is to include the ? symbol at the beginning of the string. That means the ? symbol will be included in one of the key-value pairs. To get around this you can add a & symbol after the ? symbol (and before the first key-value pair) and the server will think the ? is part of a malformed key-value pair and hopefully discard it. So you'll use something like this:

string data ="?&username=17090099526&password=rowan";

And the server should show you this:

{"username":"17090099526","password":"rowan"}

At least, this is what I have seen in my testing, but it might be different depending on the server software. You will want to check the server output and make sure it doesn't give you something like this:

{"?":"","username":"17090099526","password":"rowan"}

Even if it does, you'll just need to adjust the code on the server or when you receive a response in FlexSim so it doesn't include the first key-value pair with the ? symbol.

1 Like 1 ·
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.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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