Registry

Written by Kevin Cloutier on Feb 26, 2024 3:01 pm
{{ctrl.getFunctions()}}

Functions

Here is an overview of the available Registry Functions:

  • readRegistryKey (keyName, callback) – Executes the callback when the given registry key’s value is retrieved from the JNIOR
  • value = readRegistryKeyAsync (keyName) – Gets the value of a registry key.  Since this function is a async function, it will wait for the result before returning.
  • readRegistryKeys ([keyArray], callback) – Executes the callback when the given registry key’s values are retrieved from the JNIOR
  • registrySubscription (key, callback) – Executes the callback when the given registry key’s value is retrieved from the JNIOR. The callback is also executed any time there is an update to that registry key value

Read Registry Key

Reading a Registry Key is an asynchronous operation.  We don’t want our page to wait for the response, even though it will be quick.  So again, we need to supply a callback.  Our method syntax is readRegistry(keyName, callback);

This may cause you to structure your application differently if you are waiting for one key’s value before executing another code.

Syntax

// With Callback Function
readRegistryKey (keyName, callbackFn)

// With Inline Callback 
readRegistryKey (keyName, function(keyName, value){ /* ... */ })

Parameters

keyNamestring – The name of the registry key that we want the value of

callbackFnfunction – The function to execute when the value is returned. This can be a reference to a function or an inline anonymous function.

        keyNamestring – The name of the registry key being returned

        valuestring – The value of the registry key being returned

Return Value

None

Example

    jnrWebsocket.readRegistry( '$SerialNumber', function (keyName, value) {
        this.serialNumber = value;
        alert('The Serial Number is: ' + this.serialNumber);
    });

Read Registry Key Async

It would be easier if we could wait for the response without supplying a callback.  Well, now we can for a single key request.  We will call the readRegistryKeyAsync() method.  But it does involve a little more care.  The use of async / await will help us do that.  The await operator must be used in a async method.  It cannot be used in a normal method.  Here is how we would do that.

Syntax

value = readRegistryKeyAsync (keyName)

Parameters

keyNamestring – The name of the registry key that we want the value of

Return Value

valuestring – The value of the registry key being returned.  Applies to the Async version only.

Example

        var getJniorInfo = async function () {
            console.log('serial: ' + await jnrwebsocket.readRegistryKeyAsync('$serialnumber'));
            console.log('model: ' + await jnrwebsocket.readRegistryKeyAsync('$model'));
            console.log('boot time: ' + await jnrwebsocket.readRegistryKeyAsync('$boottime'));
        };

        jnrwebsocket.addOnLoggedInListener(getJniorInfo);

Read Registry Keys

More than likely you will need to read more than one key.  Currently, we have the following code where an array of registry keys is supplied.  The callback is then executed for each key and value returned. 

    var registryKeys = ['$serialnumber', '$model', '$boottime'];
    jnrwebsocket.readRegistryKeys(registryKeys, function (key, value) {
        if ('$serialnumber' == key) SerialNumber = value;
        else if ('$model' == key) Model = value;
        else if ('$boottime' == key) BootTime = value;
    });

In this case, the callback gives you the value but you have work to do to assign or use it correctly.  The real benefit comes in when thought is given to the jnior and the amount of communication that goes on.  It is better to send one request and get one response.  The savings go up with the number of keys requested.

Can this be handled better on the client side?  I’m sure it can.  But how?

What if the callback returned an object of key : value pairs?

    var registryKeys = ['$serialnumber', '$model', '$boottime'];
    jnrwebsocket.readRegistryKeys(registryKeys, function (keys) {
        SerialNumber = keys['$serialnumber'];
        Model = keys['$model'];
        BootTime = keys['$boottime'];
    });

Registry Subscription

Hey, a registry key may change sometime in the future.  I want to know when that happens.  Do I have to keep asking to find out if this happens?

No, you don’t.  You need to only subscribe to the registry key.  Let JANOS and the JNIOR do the rest.  JANOS will keep track of the connection that cares about the registry update and alert it accordingly.

The method declaration and use mimic that of the readRegistryKey() method with the only change being that the callback will get called any time that the registry key changes.

Example HTML File

<html>

<head>
    <script src="Integ/comm.js"></script>
    <script src="Integ/md5.js"></script>
    <script src="Integ/base64.js"></script>
    <script src="Integ/jnrWebsocket.js"></script>

    <script>
        var jnrwebsocket = new JnrWebsocket();
        jnrwebsocket.connect('ws://10.0.0.155');
        jnrwebsocket.enableCommLogging();
        jnrwebsocket.addOnLoggedInListener(function () {

            /** Below is an example of using a callback function called readRegistryKey that gets
            executed when the result of the registry is read from the JNIOR. The parameter for this
            function is the name of the registry key you wish to read. The value returned is the
            value of the registry key. This example reads the registry key $SerialNumber from the
            JNIOR. */
            jnrwebsocket.readRegistryKey('$SerialNumber', function (keyName, value) {
                var serialNumber = value;
                console.log(serialNumber);
            });

            /** Below is an example of using a callback function called readRegistryKeys that gets
            executed when the result of mulitple registry keys are read from the JNIOR. The parameter
            for this function is and array of registry keys you wish to read. The values returned are
            the values of the registry keys. This example reads the registry keys $SerialNumber,
            $Model, and $BootTime from the JNIOR. */
            var registryKeys = ['$SerialNumber', '$Model', '$BootTime'];
            jnrwebsocket.readRegistryKeys(registryKeys, function (keys) {
                console.log(keys['$serialnumber']);
                console.log(keys['$model']);
                console.log(keys['$boottime']);
            });

            /** Below is an example of using a Async function called readRegistryKeyAsync that blocks
            and waits to execute until the result of mulitple registry keys are read from the JNIOR. 
            The parameter for this function is and array of registry keys you wish to read. The values
            returned are the values of the registry keys. This example reads the registry keys 
            $SerialNumber, $Model, and $BootTime from the JNIOR. */
            (async function () {
                console.log('serial: ' + await jnrwebsocket.readRegistryKeyAsync('$SerialNumber'));
                console.log('model: ' + await jnrwebsocket.readRegistryKeyAsync('$Model'));
                console.log('boot time: ' + await jnrwebsocket.readRegistryKeyAsync('$BootTime'));
            });

        });
    </script>
</head>

</html>
On this page