Is there a httpget similar method for Javascript?

JK
Jacob Kirkwood
Harris Teeter

Hello, I am working on something that requires me to download a file, read the file contents (3 Lines of data) and report back to the Console as Custom Data. Currently I do not see a way within the Javascript documentation to do this without having to do a mixed script with Legacy (httpget) and Javascript. I did see a previous discussion post from two years ago but I figured I would ask again as some time has passed. I would prefer to find a way to get this in one script with JavaScript.

Currently we are on SOTI 2024.1 and use Zebra TC52/TC53E devices.

How I want the script to work

Pulls the file down -> Read the lines and extract a Store number -> Take that value and insert it into an XML file that lives on the device -> Custom Data reports that value on Check In/Sync 

Thanks!

a month ago
SOTI MobiControl
ANSWERS
RS
Robert Schäfer
a month ago (edited a month ago)

Hi Jacob,

Interesting requirement, i like it!

You you can't do this with just the core javascript engine, not currently anyway. No way to make an API request or ftp (like you can with a legacy script).

For now i think you will have to go with a mixed approach.

I realise this isn't necassery your question and perhaps you have the knowhow to build this yourself, but if you can give me more context i can help:

  • How does the process start, is this triggered periodically, when the device starts up or something else?
  • Where is the file stored?
  • Are devices in a kiosk?

 

 

JK
Jacob Kirkwood
a month ago

Hey Robert! 

Thank you so much for the quick reply! 

Currently, I have preset custom attributes assigned to device groups so devices recieve a particular identifier for the group they are in.
Our devices utilize IP relocation to change groups and when it does a Signal Policy is triggered and runs a script that:

  1. Pulls the corresponding custom attribute.

  2. Checks if a specific file exists on the device (located at /sdcard/CustomData/).

    • If it doesn’t exist, the script creates it and writes the custom attribute value to it along with the folder.

  3. Custom Data then monitors that XML file and reports the data back to the console.

This setup lets us use the value both as:

  • A Custom Attribute (for device renaming via another script in the same policy), and

  • Custom Data (for reporting purposes).

However, I’ve noticed that custom attributes don’t update as quickly as needed. In testing, the attribute often lags behind by one group move — likely due to propagation delays from the console.

Using a httpget call to our internal API, it can determine the device’s current location dynamically based on IP address. This would eliminate the need to rely on the static custom attributes in the console, as the device could retrieve its location data directly at runtime (still triggered on group move).

All devices are locked down, and the file handling remains within /sdcard/CustomData/.

RS
Robert Schäfer
a month ago

Hi Jacob,

Thanks for the details, couple of questions just so i fully understand:

  1. Whats the value stored in the custom attribute, is this the storeID or something else?
  2. Are your groups organised by store?
  3. If your answer to the above two points is yes, can you explain why you want to dynamically retrieve the storeID if you already have this stored in a custom attribute at a group level?

As for the delay when moving groups, i have experienced something similar. In fact in my case it was so bad in many cases custom attributes were simply not pushed to the device. This was an issue with signal policies and we upgraded to 2024.1.2 which fixed the issue. Im not entirely sure if you are on 2024.1.1 or 2024.1.2, but perhaps thats something to look into. If you are already on 2024.1.2 then the behaviour you describe may be something else and i would raise that with SOTI.

Even after the upgrade we still had scenarios where it took 15-20 seconds before the attribute arrived on the device after a group move and so i implemented a delay and a loop to check if the custom attribute was present (that part of course only works if your going from a group with an attribute to one without).

The reason i asked whether your devices were in a kiosk, is that you could look at triggering an api call by including javascript in your kiosk template. You can also call soti's agent javascript like so:

function sendJavascript(command, callback){
            var encodedCommand = encodeURIComponent(command);
            var request = new XMLHttpRequest();

            request.open("GET", "mcscript://" + encodedCommand);
            request.addEventListener("load", function() {
                callback(this.responseText);
            });
            request.send();
        }

Triggering that on group move might be tricky but thought i would mention it.

RS
Rafael Schäfer
a month ago

I want to add a question:

If you already push a file to the device containing the data, why not already in the relevant (ini or xml) format?

From my side i maybe could help here only regarding ini file as we use this for a similar purpose but we generate it on enrollment on device side already.

JK
Jacob Kirkwood
a month ago

Hello!

Our goal is to be able to do quick and clean reporting, while also ensuring devices are automatically renamed based on their current location whenever IP relocation moves them. (This can happen when new devices are added or when associates bring devices from one store to another.) 

We initially used Custom Attributes as a starting point. However, due to lag, devices were sometimes renamed using outdated attribute values instead of the current values inherited through the group. This delay is likely due to syncing between the device and the console. To address this, we want the process to complete in one seamless execution. ( We are on 2024.1.2.1046 )

We’ve been comparing Custom Data and Custom Attributes to determine which better suits our use case, and we’re leaning toward Custom Data since it appears to be more reliable.

I got our scripts functioning as intended and have them in a Signal Policy to trigger automatically upon group movement. Testing so far shows the reporting works exactly as expected. Here’s a summary of what happens on the script side in the signal policy:

  • Retrieve the locationData.txt file via httpget and save it to the SD card.
  • Verify the file exists, split the contents, and store the store number in a variable.
  • Check if the required XML file exists; if not, create a new one with the proper structure and values.
  • Once the XML is created, use the same value to send a device rename command and rename the device accordingly.

The steps above are executed using one Legacy script and one JavaScript script. 

To address some previous questions:

  • The device groups do include a version of the store number, but for reporting purposes, we want something cleaner and more consistent. 
    • Example: \Company\Devices\EST\Stores\DEVICE_TYPE\DEVICE_TYPE_D00\000 vs. S000 
  • When we call the httpget, the data comes in a raw format and must be parsed to extract the needed values. 
    • Example of returned data (regardless of file format):
      • DIVISION=000
      • STORE=00000
      • STORENAME=S000 

I also never thought about using API calls within the lockdown and that could be very beneficial.

RS
Rafael Schäfer
a month ago

I never used/tried it but in JS you should be able to do http get like this (ofc a lot to build around the result and so on still needed):

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}

Source: https://stackoverflow.com/questions/247483/http-get-request-in-javascript

And then no need for a kiosk, just use a task profile after you verified your script, you send to the test device manually works fine.

Take the result of the http get then and continue working on it before saving it as a file to the sdcard folder.

MD
Matt Dermody Diamond Contributor
a month ago

I'm assuming the file (locationData.txt) is a lookup table of sorts containing IP subnet ranges mapped to Store Numbers. If so, do you need to actually have this file downloaded at script run time via an httpget call, or could you have the file delivered to the device via Package and/or File Sync rule so it is already present on the device? You are already checking to see if the XML for Custom Data is present on the device, you could have a similar check built in that verifies if the locationData.txt file is already present (delivered separately). You'd have to update that file via Package or File Sync every time a new store is added or an IP subnet range changes in that situation but it should otherwise work for your situation. 

N
NMMOD@SOTI
13 days ago

Hi Jacob Kirkwood,

Thanks for posting on SOTI Pulse.

I hope the suggestions provided by Matt, Robert and Rafael have helped you answer your query. Please inform us if you require further assistance.

Additionally, if any response has helped address your inquiry, we kindly request you to mark it as "is solution" so that others may also benefit from this information.

Thank you for choosing SOTI.