Node.js® TypeScript Module Example Implementation

Setting up a TypeScript Module

To create a new TypeScript module, proceed as follows (assuming that Node.js® is installed).

  1. Decide on a name for the Node.js® module you want to create (called <modulename> in the following.)
  2. Copy this directory into a project in directory javascript/<modulename>
    Note: In case the copied files are read-only, it is necessary to enable write access using OS tools
  3. In javascript/<modulname>/package.json, replace the value for "name" with the name of the module, which should be the same as the directory name by convention, e.g.:
    {
     "name": "<modulename>",
     ...
    }
  4. Open a command prompt in the copied javascript/<modulename> directory.
  5. Call the following command to install required modules including a path to the winccoa-manager type definitions (which are needed by the TypeScript compiler).
    npm install --save-dev <path-to-installation>/javascript/@types/winccoa-manager
    Note: Currently, the TypeScript plug-in for EsLint requires an EsLint version that uses deprecated packages, so expect to see some warnings when installing these packages.
  6. Transpile TypeScript files to JavaScript by running the following command from the copied directory:
    npx tsc
    Or start a watcher, which will automatically transpile TypeScript files when they are changed:
     npx tsc -w

    It will generate index.js in this directory, which will be used by JavaScript manager in the next step.

  7. Add a JavaScript Manager to the console with the path of index.js in the copied directory as its only parameter (relative to javascript, e.g.: <modulename>/index.js). Set Start mode to manual.
    Figure 1. Add a JavaScript Manager
  8. Start the manager - it will terminate immediately.
  9. Check that the message "JavaScript Manager for WinCC OA working" is shown in the Log Viewer.
  10. Start developing. Below you can find the TypeScript module example:
    // The command line for the Node.js® manager must contain the path to
    // the transpiled file for this file relative to data/nodejs, e. g.:
    //
    // modulename/index.js
    //
    // NB: since the transpiled file must be used by the manager, the
    // extension is .js, not .ts
    
    // import WinCC OA interface
    import { WinccoaManager } from 'winccoa-manager';
    const winccoa = new WinccoaManager();
    
    // main function
    async function main() {
      const dpeName = 'ExampleDP_Arg1.';
      const value = await winccoa.dpGet(dpeName);
    
      console.info('Node.js® manager for WinCC OA working');
      console.info('Value of ' + dpeName + ' = ' + value);
    
      winccoa.exit(0);
    }
    
    // start the main function
    main();
    Note: TypeScript code must be called from inside a method or function to prevent unexpected or undefined behavior.
  11. (optional, but recommended) Check and format your code with:
    npm run lint
    npm run format