nodewire

NodeWire - IoT Framework

Usage no npm install needed!

<script type="module">
  import nodewire from 'https://cdn.skypack.dev/nodewire';
</script>

README

Creating a node

import {nw, Node} from 'nodewire'

let thenode;
let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImFobWFkLnNAZmF4dGVyLmNvbSIsImluc3RhbmNlIjoiaHgzZjN0Yzh6Y3Q3In0.cHdgXdoD4_Fkr-W0KRhoXJdsgbY-c6rXfbsuUaGgmwg";

nw.once('gack', ()=>{
    thenode = new Node('mynode', {
        inputs: 'start reset',
        outputs: 'count status',
        init: node => {
            setInterval(()=>node.handler.loop(node), 1000);
        },
        onReset: (node, val) => {
            node.count = val;
        },
        getStatus: node => {
            return {
                count: node.count,
                counting: node.start===1
            };
        },
        loop: node => {
            if(node.start===1)
                node.count+=1;
        }
    }); 
})

nw.connect({token: token});
export default thenode;

Connect to a node

import {nw, Node} from 'nodewire'

let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImFobWFkLnNAZmF4dGVyLmNvbSIsImluc3RhbmNlIjoiaHgzZjN0Yzh6Y3Q3In0.cHdgXdoD4_Fkr-W0KRhoXJdsgbY-c6rXfbsuUaGgmwg";
nw.onStart(async ()=>{
    const me = new Node('control');
    let thenode = await me.getNode('mynode');
    thenode.setHandler({
        onCount: val => {
            console.log('got', val);
            if(val==10) thenode.start = 0;
        }
    })
    thenode.reset = 0;
    thenode.start = 1;
});

nw.connect({
    token: token,
    server: 'localhost'
});

ReactJs Client

import React, { Component } from 'react';
import {nw, Node} from 'nodewire';

var thenode;
 
class App extends Component {
    state = {count:0, waiting: true}
    constructor(){
        super();
        const initialize = async () =>  {
            const me = new Node('control');
            thenode = await me.getNode('mynode'); 
            this.setState({waiting: false, count: thenode.count});
            thenode.setHandler({
                onCount: val => {
                    this.setState({count: val});
                }
            })
        }
        nw.onStart(initialize);
    }

    render() {
        if(this.state.waiting)
            return (
                <div>
                    Wait ...
                </div>
            )
        return (
            <div>
                <div>
                    {this.state.count}
                </div>
                <button onClick={()=>{
                    thenode.start = 1;
                }}>Start</button>
                <button onClick={()=>{
                    thenode.start = 0;
                }}>Stop</button>
                <button onClick={()=>{
                    thenode.reset = 0;
                }}>Reset</button>
            </div>
        );
    }
}
 
export default App;

React Hooks

import React, { useState, useEffect } from 'react';
import {nw, Node} from 'nodewire';

var thenode;
function App() {
    const [count, setCount] = useState(0);
    const [waiting, setWaiting] = useState(true);
    
    useEffect(() => {
        async function initialize(){
            const me = new Node('control');
            thenode = await me.getNode('mynode'); 
            setCount(thenode.count);
            setWaiting(false);
            thenode.setHandler({
                onCount: val => {
                    setCount(val);
                }
            })
        }
        nw.onStart(initialize);
    }, []);

    if(waiting)
        return (
            <div>
                Wait ...
            </div>
        )
    return (
        <div>
            <div>
               The Count is: {count}
            </div>
            <button onClick={()=>{
                thenode.start = 1;
            }}>Start</button>
            <button onClick={()=>{
                thenode.start = 0;
            }}>Stop</button>
            <button onClick={()=>{
                thenode.reset = 0;
            }}>Reset</button>
        </div>
    );
}
export default App;

issues

  1. when node goes offline, messages should be queued for when it comes back online
  2. monitor delivery, and queue if not delivered

node.setportvalue: if state == disconnected: save cmd else: send and save in temp if val not received in 3 seconds: chnage state to disconnected save tmp

on(gack): for node in nodes: node.set_state = connected node.send_saved_cmds