1.2.7 • Published 4 years ago

nw_node v1.2.7

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Introduction

NodeWire is a framework for building IoT devices and software that works with them. This library allows you to build nodejs applications that can seamlessly communicate with NodeWire based devices.

NodeWire supports a dataflow/message-driven programming paradigm. It extends the reactive paradigm to distributed applications that are interconnected - also known as microservices. Every NodeWire app must implement a node interface. A node is a virtual appliance that has input and output ports. The inside of a node is a state machine and a node interacts with other nodes only through its input and output ports.

To read more about NodeWire please visit http://www.nodewire.org

Creating a node

First create an app:

$ mkdir mynode
$ cd mynode
$ npm init

Next, install nodewire:

$ npm install nw_node

Create your source file, 'app.js' and paste the following code in it:

var nw = require('nw_node').nw;
var node = require('nw_node').node;

const conf = {                          // this will work but you should replace it with your own nodewire account credentials
    username:"test@microscale.net",    // register an account at dashboard.nodewire.org
    password:"aB01@",
    instance:"1jex2k7cbedg",
    server:"dashboard.nodewire.org",
}
nw.debug_level=0 // 0 = no debug messages, 1 = only important messages, 2 = all messages
nw.connect(conf);
nw.once('gack', async ()=>{
    let the_node = new node('test', {inputs: 'start reset', outputs: 'count'}); // create the node. the parameters aree the node name and the list of input and output ports

    the_node.count = 0; // initialize the port values, this is optional
    the_node.reset = 0;
    the_node.start = 0;
 
    the_node.when('reset', count=>the_node.count=count);  // respond to incoming port value

    setInterval(() => {            // the internal logic of our node: count if the start port is equal to 1
        if(the_node.start===1)
        {
            the_node.count += 1;
            console.log(the_node.count);
        }
    }, 1000);
});

Next run the code:

$ node app.js

Nothing significant will happen.

Now lets create another project, so we can control the node remotely:

Monitoring and Controlling the node

Open another command/shell window and type the following:

$ mkdir myapp
$ cd myapp
$ npm init

Next, install nodewire:

$ npm install nw_node

Create your source file, 'app.js' and paste the following code in it:

var nw = require('nw_node').nw;

const conf = {
    username:"test@microscale.net",
    password:"aB01@",
    instance:"1jex2k7cbedg",
    server:"dashboard.nodewire.org",
}
let thenode;
nw.debug_level=0 // 0 = no debug messages, 1 = only important messages, 2 = all messages
nw.connect(conf);
nw.once('gack', async ()=>{
    console.log('connected');
    thenode = await nw.getnode('test');  // get the node

    nw.when('test.count', (count)=>{  // monitor port changes
        console.log(count);
        if(count===10) thenode.reset = 20; // jump to count 20
        if(count==30){                // stop the count when count is 30
            thenode.start = 0;
            console.log('stop count');
        }
    });

    setTimeout(()=>{
        console.log('start count');
        thenode.start = 1;  // start count after 1 second
    }, 2000);
});

now, run application:

$ node app.js

You will the the two applications interacting with each other.

NodeWire Signals

NodeWire is based on an event driven paradigm. You always have to monitor signals in order to respond to them. And most of the code will be invalid if not executed after a certain event, most importantly after the 'gack' signal is received.

This signal signifies that the connection is fully established and all system parameters have been initialized.

Most signals are made of nodename followed by a period and then a portname: 'nodename.portname'. And it is activated whenever the corresponding portvalue changes.

There are two ways to monitor a signal, either by using the 'once' function which monitors the signal only once and then ignores subsequent occurrences. Or by using the 'when' function which continues to monitor the signal indefinitely. Both of these functions can be used simultaneously.

Controlling our node from the NodeWire dashboard

Using the NodeWire Database to store values

Using NodeWire with ExpressJs

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago