Node Examples/en: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
Zeile 98: Zeile 98:


[[Bild:OSM_TestAction.png|thumb|300px|Test/Demo Network]]
[[Bild:OSM_TestAction.png|thumb|300px|Test/Demo Network]]
Notice the addition of the "wait()" call at the end, which tells expecco, that an asynchronous callback is expected, and the action has not yet finished, when "execute()" returns.
[[Datei:point_right.png|20px]] Notice the addition of the "wait()" call at the end, which tells expecco, that an asynchronous callback is expected, and the action has not yet finished, when "execute()" returns.
Also notice the calls to "success()" and "error()" in the callbacks to tell expecco, that the action has finished.
Also notice the calls to "success()" and "error()" in the callbacks to tell expecco, that the action has finished.



Version vom 17. Oktober 2019, 00:21 Uhr

Node action blocks make it easy to interface a Node.js interpreter, and execute functions inside of it. Node.js is an open source interpreter for JavaScript. For details on the language and libraries, visit "NodeJS Tutorial".

There are many useful interfaces for which Node implementations are readily available and creating an interface to them is a matter of minutes of work.

This document gives a few examples.

Prerequisites[Bearbeiten]

You have to download and install "node" separately - the interpreter is not part of the expecco installation procedure (see https://nodejs.org/en/download ).

If "node" is not found along your shell's path (%PATH% on windows), you have to define the location of the node executable in the "Extras" → "Settings" → "External Interpreters" dialog. Click on the "?" icon to check if it is executable (the node's version should be displayed at the bottom).

Also, unless your node packages are installed globally or in your home folder's "node_modules", you should also add the path to your desired node_modules folder there.

Checking if Node Action Block Execution Works[Bearbeiten]

Simply create a new "Node (bridged)" action via the treemenu, and execute it. This will fail (red-status) if any of the above was configured incorrectly. If you get green, you should be ready to run!

Example: Getting a Map from Open Street Map[Bearbeiten]

In this example, we will use a node package called "staticmaps", which generates streetmaps give a geo location. You find the example in the demos folder in "d60_NodeJS_OpenStreetMapAccess.ets" ( also found here: Datei:OSM Demo.ets ).

  • First, install the node package, by entering on the command line (i.e. cmd-window or shell window):
npm install -g staticmaps

Take a look at the package's web site https://www.npmjs.com/package/staticmaps for detail information and more examples.

First Step: Try the Interface[Bearbeiten]

As a first quick&dirty test, we'll take one of the examples found on the website, paste it unchanged into an action block and execute it.

This will ensure, that all required packages are correctly installed, and that the underlying webservices are reachable. If this "Smoke Test" does not work, it won't make much sense to continue. Instead, you should consult the node-package's documentation and look for updates, patches and more info.

  • create a new "Node (bridged)" action
  • define its code by pasting in the original example code from the website inside the action's execute() function:
var StaticMaps = require("staticmaps");

function execute() {

   var options = {
     width: 600,
     height: 400
   };
    
   var map = new StaticMaps(options);

   const zoom = 13;
   const center = [13.437524,52.4945528];

   map.render(center, zoom)
     .then(() => map.image.save('/tmp/center.png'))  
     .then(() => console.log('File saved!'))
     .catch(function(err) { console.log(err); });
}
  • run this action
  • verify: a file named "center.png" should now be found in the "tmp" folder, and it should present a map of some Berlin-Center area (you can open a FileBrowser or a WindowsExplorer on it).

Second Step: Add Pins to Make the Action Universal[Bearbeiten]

The above code had all parameters hardcoded. Of course, we like those to be parameters of the action block. Add the following pins:

  • longitude (with type Float)
  • latitude (with type Float)
  • zoom (with type Integer)
  • width (type Integer)
  • height (type Integer)
  • imageFileName (type Filename)
  • and change the code to fetch the pin values instead:
var StaticMaps = require("staticmaps");

function execute() {

   var options = {
     width:  width.value(),
     height: heigth.value()
   };
    
   var map = new StaticMaps(options);

   const center = [ longitude.value(),latitude.value()];

   map.render(center, zoom.value())
     .then(() => map.image.save(imageFileName.value()))  
     .then(() => success())
     .catch((err) => error(err));
   wait();
}
Test/Demo Network

Point right.png Notice the addition of the "wait()" call at the end, which tells expecco, that an asynchronous callback is expected, and the action has not yet finished, when "execute()" returns. Also notice the calls to "success()" and "error()" in the callbacks to tell expecco, that the action has finished.

  • then, in the "Test/Demo" page, give the corresponding pins the values:
  • longitude: 9.0367885
  • latitude: 48.7791238
  • zoom: 10
  • width (Integer)
  • height (Integer)
  • imageFileName ("/tmp/anyName.png")

You can also add steps to fetch the resulting file and show it, as done in the test/demo network. Of course, Windows users should change the pathname of the temporary file to "c:\temp\anyName.png")

When you run it, the map of Stuttgart should be shown.



Copyright © 2014-2024 eXept Software AG