Javascript not showing in logs at all

Solved
TE
Travis Epperson
HD Supply - STOCK-CSC

I have been trying to get a java script to run on Zebra devices running Android 10.  Debugging has been difficult because the logs will often show NOTHING.  Or if it does show the logs show as a timestamp that is older then when the script was pushed.  Can anyone see anything wrong with this script?  I'm trying to run this after a file sync to only do a firmware update if certain firmware is already on the device and if the files are both there.  I have been pushing this from the console for testing, and I remove the first line when pushing from the console.

-------------

#!/usr/bin/env js
var BuildNumber = new String(mobicontrol.os.buildnumber);
var FirmwareBase = new mobicontrol.io.File('/sdcard/HE_DELTA_UPDATE_01-23-18.00-OG_TO_01-30-04.U00-OG.zip');
var FirmwareLG = new mobicontrol.io.File('/sdcard/HE_DELTA_UPDATE_01-30-04.00-OG-U44-STD.zip');
if (BuildNumber != "01-30-04.00-OG-U44-STD") {
    if (FirmwareBase.exists==true && FirmwareLG.exists==true) {
        if (BuildNumber.includes("01-23-18")) {
            mobicontrol.log.info("Installing Firmware Base");
            installsystemupdate(FirmwareBase);
        } else if (BuildNumber.includes("01-30-04")) {
            mobicontrol.log.info("Installing Firmware LifeGuard");
            installsystemupdate(FirmwareLG);
        }
    } else {
        mobicontrol.log.error("Sync not finished, Firmware files do not both exist on the device yet");
    }
} else {
    mobicontrol.log.error("The device is already on the correct Firmware, no upgrade needed");
}

2 years ago
SOTI MobiControl
ANSWERS
RC
Raymond Chan Diamond Contributor
2 years ago

I have not done any test of your program on any Zebra device, but I believe the very first comment from Matt is valuable and might shed some light on the cause of your problem of not getting any log message.  Using the virtual group filtering approach Matt suggested  has the definite advantage of saving network bandwidth/cost for transferring the two files to irrelevant devices in the first place, though using a working javascript as another line of defense is even better.

By just looking at your program source codes, I notice that there is one missing program flow branch (i.e. when the build number does not include the two patterns tested against) in the innermost nested-if structure that of course has no code to generate log (at least for your debugging).  I suggest you to add this missing final "else' branch with some observable action such as

   mobicontrol.log.error("Current build number not targeted for upgrade by current script!" );

Also, you have to check the version of the device agent running on your Zebra devices to confirm that they are recent enough to support all the required javascript API's you have used.

Finally,  regarding your problem of having logs that show timestamp that is older than when the script was pushed, maybe you should check if the device time settings are correct, and whether they are stray logs left from previous execution during debugging phase.  You of course should also do some tests on the time accuracy of the log entry timestamp for your device agent version(s) with a much simpler unconditional script.

Solution
TE
Travis Epperson
2 years ago

Thanks Raymond,

I do have the proper API (that was an early problem I had).  I agree I missed an output without comment, that was my problem!  Thanks so much!!

MD
Matt Dermody Diamond Contributor
2 years ago

The first thing that jumps out at me is that you mentioned Zebra devices running Android 10, but these are BSP versions for Android 8. 01-23 and 01-30 are Android 8 builds, not Android 10. 

Next, I am not aware if the post-sync scripting in File Sync rules even support Javacript based scripts. If they do then that is news to me. 

And finally, I personally would recommend using File Sync rules targeted at Virtual Groups for OS Updating Zebra devices. You can have very granular Filter Criteria applied to the Virtual Groups in order to only update the devices that match very specific criteria. You should be able to accomplish everything that you're looking to accomplish with this javascript using that approach from what I can tell. 

TE
Travis Epperson
2 years ago

You are right it's for Android 8.  I appreciate the suggestion of file sync and I have done that in the past.  I was looking for an easier more flexible way to control the upgrade process.  I have 5 different models and managing firmware on all of them through dynamic filter groups feels messy and limited.  I thought the Javascript method would give me more flexibility to add variables as I went.  I'll look back at the filter groups again though.

RS
Rafael Schäfer
2 years ago

Something i would do is, to seperate the script from the file transfer and put just a script like following
install_system_update /sdcard/HE_DELTA_UPDATE_01-30-04.00-OG-U44-STD.zip
as a legcy script to the device and put an assignment criteria (Example: OEM Version is 01-30-04.U00-OG) to 
it and also I recommend to do it, like Raymond already said, use Filter groups to assign it.

Then you can prepare devices with the files way before you want them being upgraded with your script.

TE
Travis Epperson
2 years ago

Thanks Rafael,

I appreciate the suggestion.  I think that I will have more flexibility and more control over variables by running the JavaScript on the device itself.  

TE
Travis Epperson
2 years ago

For anyone stumbling on this later, my second issue is that mobicontrol.os.buildnumber should be mobicontrol.os.buildNumber.  This is why I'm not a Dev...

TE
Travis Epperson
2 years ago

For reference, here is my completed (not fully tested) script.  The flexibility in the logging and checks are why I am moving in this direction.

//TC57 details, make changes here when changing firmware versions
var TC57FirmwareBase = "HE_DELTA_UPDATE_01-23-18.00-OG_TO_01-30-04.U00-OG.zip";
var TC57FirmwareLG = "HE_DELTA_UPDATE_01-30-04.00-OG-U44-STD.zip";
// This is what is expected if a Base update is needed
var TC57Expected4Base = "01-23-18";
// This is what is expected if an LG update is needed
var TC57FinalFirmware = "01-30-04.00-OG-U44-STD";


// TC57X details, make changes here when changing firmware versions
var TC57XFirmwareLG = "HE_DELTA_UPDATE_10-16-10.00-QG-U133-STD-HEL-04.zip";
var TC57XFirmwareBase = "HE_FULL_UPDATE_10-16-10.00-QG-U00-STD-HEL-04.zip";
// This is what is expected if a Base update is needed
var TC57XExpected4Base = "10-12-13";
// This is the Final Goal Firmware
var TC57XFinalFirmware = "10-16-10.00-QG-U133-STD-HEL-04";


// General Variables
var TC57Expected4LG = TC57FinalFirmware.substring(0,TC57FinalFirmware.indexOf(".")); //Grabs the base part of the final firmware for LG
var TC57XExpected4LG = TC57XFinalFirmware.substring(0,TC57XFinalFirmware.indexOf(".")); //Grabs the base part of the final firmware for LG
var BuildNumber = mobicontrol.os.buildNumber;
var Expected4Base = "";
var Expected4LG = "";
var FinalFirmware = "";
var Firmware;

// Setup variables depending on device Model
if (mobicontrol.device.model == "TC57") {
    var FirmwareBase = new mobicontrol.io.File('/sdcard/' + TC57FirmwareBase);
    var FirmwareLG = new mobicontrol.io.File('/sdcard/' + TC57FirmwareLG);
    Expected4Base = TC57Expected4Base;
    Expected4LG = TC57Expected4LG;
    FinalFirmware = TC57FinalFirmware;
    // Run Firmware Update
    FirmwareUpdateCheck()
} else if (mobicontrol.device.model == "TC57X") {
    var FirmwareBase = new mobicontrol.io.File('/sdcard/' + TC57XFirmwareBase);
    var FirmwareLG = new mobicontrol.io.File('/sdcard/' + TC57XFirmwareLG);
    Expected4Base = TC57XExpected4Base;
    Expected4LG = TC57XExpected4LG;
    FinalFirmware = TC57XFinalFirmware;
    // Run Firmware Update
    FirmwareUpdateCheck()
} else {
    mobicontrol.log.error("This firmware upgrade script is not for this Model Device " + mobicontrol.device.model);
}

// This makes sure that the firmware update only happens on Sat or Sun or from 12am - 4am
function AfterHoursCheck() {
    var d = new Date(); // current time
    var hours = d.getHours();
    var mins = d.getMinutes();
    var day = d.getDay();

    return (day === 6 || day === 0) || (hours >= 0 && hours < 4);
}

// First makes sure that the current firmware can be upgraded with this script, checks to see if the device needs Base or LG, Makes sure the file is on the device, and finally makes sure that it's after hours before running.
function FirmwareUpdateCheck() {
    if (BuildNumber != FinalFirmware) {
        if (mobicontrol.battery.level >= 0.35) {
            if (BuildNumber.includes(Expected4Base) || BuildNumber.includes(Expected4LG)) {
                if (BuildNumber.includes(Expected4Base)) {
                    if (FirmwareBase.exists==true) {
                        if (AfterHoursCheck()){
                            Firmware = FirmwareBase
                            UserAproval()
                        } else {
                            mobicontrol.log.info("Firmware update skipped because of AfterHoursCheck");
                        }
                    } else {
                        mobicontrol.log.error("Sync not finished, Base Firmware file does not exist on the device yet");
                    }
                }    
                if (BuildNumber.includes(Expected4LG)) {
                    if (FirmwareLG.exists==true) {
                        if (AfterHoursCheck()){
                            Firmware = FirmwareLG;
                            UserAproval()
                        } else {
                            mobicontrol.log.info("Firmware update skipped because of AfterHoursCheck");
                        }
                    } else {
                        mobicontrol.log.error("Sync not finished, LG Firmware file does not exist on the device yet");
                    }
                }    
            } else {
                mobicontrol.log.error("OEM version is not in scope for this firmware upgrade " + mobicontrol.os.buildNumber);
            }
        } else {
            mobicontrol.log.error("Battery level to low for upgrade " + mobicontrol.battery.level);
        }
    } else {
        mobicontrol.log.error("The device is already on the correct Firmware, no upgrade needed");
    }
}

//Message popup warning that the device is about to start a firmware update.  Gives the user the ability to delay until the next time the script is run.
function UserAproval() {
    mobicontrol.message.createTextPromptDialog('Firmware Upgrade starting. (The Device will be unusable for 30 min, do not remove battery!)')
        .withTitle("Firmware Upgrade")
        .withNotification()
        .withDefaultText("Would you like to delay?")
        .withButtons('Continue', 'Delay')
        .withTimer(30000)
        .withCallback(FirmwareUpdate).show();
}

//This is triggered once the user prompt is gone.  If the user did not delay then the device is updated
function FirmwareUpdate(result) {
    if (result.buttonLabel != "Delay") {
        mobicontrol.log.info("Installing Firmware");
        installsystemupdate(Firmware);
    } else {
        mobicontrol.log.error("User Delayed");
        mobicontrol.message.createTextPromptDialog('Firmware Upgrade Delayed')
        .withTitle("Delayed") 
        .withTimer(10000)
        .show()
    }
}