4.4. Modifying Data

DataHub scripting lets you modify data as it passes through the DataHub. This example script makes a linear transformation, writes the results to a different DataHub point, and incidentally prints the value of point and the conversion.

  1. Create a new script whose main class is called 'ModifyData'. Here's how.
  2. Change the name of .samplemethod to .convert and edit it like this:
    method ModifyData.convert (pt1, !pt2, multiplier, adder)
    {
        local output;
        set(pt2, ((pt1 * multiplier) + adder));
        princ(format("The sine is: %2.3f    Converted it is: %2.3f\n",
                     pt1, eval(pt2)));
    }
    
    This method does the conversion and prints the output. Notice that the pt2 argument has an exclamation point (!) in front of it. This protects the argument from being evaluated, because we only want the point name, not its value. Please refer to Function Arguments in the Gamma manual for more information.
    We need to assign a value to the point, but keep the variable name associated with the DataHub point, not the value. This requires the set function, because using the equals sign would just assign the value to the variable, and it would never reach the DataHub point.
  3. Edit the .constructor method to look like this:
    method ModifyData.constructor ()
    {
        multiplier = 3;
        adder = 5;
        
        if (undefined_p($default:ConvertedSine))
            datahub_command ("(cset default:ConvertedSine \"\")", 1);
    
        .OnChange(#$DataSim:Sine, 
                  `(@self).convert($DataSim:Sine, 
                                   $default:ConvertedSine, multiplier, adder));
    
        after(3, `destroy(@self));
    }
    We don't make the multiplier and adder variables local because they'll be used by the Application class's .destructor method. We use the datahub_command function call the cset function to have the DataHub create the ConvertedSine point and set its value to an empty string. We then use the inherited .OnChange method to set up the .convert method.
  4. The script is ready to run. Open the Data Browser window right-clicking the DataHub icon in the System Tray and selecting View Data from the pop-up menu. Select the default domain. Also make sure the DataSim program is running and the Script Log is open.
  5. Run the script. It should run for 3 seconds, writing data to the Script Log, and changing the values to the point ConvertedSine in the default domain.
    Of course, the data printed in the Script Log is just for convenience. You could comment out the princ statement, or remove it altogether. The main thing is the data updating in the Data Browser.