BitsToInt.g

BitsToInt.g — converts a set of boolean data points into an integer point.

Code

[Note]

The code for this and other example scripts can be found in the DataHub distribution archive, typically at this location:

C:\Program Files\Cogent\Cogent DataHub\scripts\

Please refer to Section 3.1, “How to Run a Script” for more information on using scripts.

/*
 * Given a list of boolean data points, construct a single integer 
 * bit field into a separate output point.  The data point list 
 * consists of a point name and the matching bit to set in the 
 * output integer. It is not necessary to supply an input data point 
 * for each bit in the output.
 */

require ("Application");

class BitsToInt Application
{
}

/* 
 * Register the mappings between input points and the output point.  
 * You can call .addBitMap as many times as you like.  If the point 
 * names follow a pattern you could write a method that constructs 
 * the two name and value arrays programmatically and then calls 
 * .addBitMap with those arrays.
 */
method BitsToInt.constructor ()
{
    .addBitMap("default:output", 
        [ "default:input0", "default:input1", "default:input2", 
        "default:input3", "default:input4", "default:input5", 
        "default:input6", "default:input7" ],
        [ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 ]);
}

/*
 * This class implements the map from input bits to an output integer.  
 * You should not need to change anything below this line.
 */

class BitsToIntMap
{
    pointNames;    // an array of point names, as symbols
    maskValues;    // an array of corresponding masks to apply to the 
                   // output when each point value is non-zero
    outputName;    // The name of the output point as a symbol
}

method BitsToIntMap.constructor(outputName, inputNames, inputValues)
{
    .outputName = symbol(outputName);
    .pointNames = inputNames;
    .maskValues = inputValues;
	
    local i;
    for (i=0; i<length(.pointNames); i++)
    {
        datahub_command(format("(create %s 1)", 
                               stringc(.pointNames[i])), 1);
        .pointNames[i] = symbol(.pointNames[i]);
    }
    datahub_command(format("(create %s 1)", 
                           stringc(.outputName)), 1);
}

method BitsToIntMap.register(app)
{
    local sym;
    with name in .pointNames do
    {
        app.OnChange(name, `(@self).recompute());
    }
    .recompute();
}

method BitsToIntMap.recompute()
{
    local value = 0, i, len = length(.pointNames), temp;
    for (i=0; i<len; i++)
    {
        if (!undefined_p(temp = number(eval(.pointNames[i])))
            && temp != 0)
            value |= .maskValues[i];
    }
    set(.outputName, value);
}

method BitsToInt.addBitMap(outputName, inputNames, inputValues)
{
    local bitmap = new BitsToIntMap(outputName, inputNames, 
                                    inputValues);
    bitmap.register(self);
    bitmap;
}

/* Start the program by instantiating the class. */
ApplicationSingleton (BitsToInt);