UNPKG

daas-sdk

Version:

nodejs wrapper for the DaaS-IoT SDK

45 lines (35 loc) 1.26 kB
// change require("../index.js") to require("daas-iot") // if you are using this code outside of this example folder const { DaasIoT, DriverTypes } = require("../index.js"); const daasApi = new DaasIoT("server"); const SID = 100; const SERVER_DIN = 101; daasApi.doInit(SID, SERVER_DIN); // Event when a new DIN is connected daasApi.onDinConnected((din) => { console.log("DIN Accepted:", din); }); // Event when a 'Message' is received daasApi.onDDOReceived((din) => { console.log("DDO received for DIN: " + din); // Make sure to locate the DIN before pulling data daasApi.locate(din); daasApi.pull(din, (origin, timestamp, typeset, data) => { console.log(`⬇ Pulling data from DIN: ${origin} - typeset: ${typeset} - data: `); console.log(new TextDecoder().decode(data)); }); }); // Enable the IPv4 driver on the server const SERVER_URL = '127.0.0.1:2101' if (daasApi.enableDriver(DriverTypes.INET4, SERVER_URL)) { console.log("Driver enabled!"); } // Mapping the client DIN so that the server will accept its requests const CLIENT_DIN = 102; if (daasApi.map(CLIENT_DIN)) { console.log("Remote Node Mapped"); } // Activate the server if (daasApi.doPerform()) { console.log("Node performed!"); }