UNPKG

daas-sdk

Version:

nodejs wrapper for the DaaS-IoT SDK

45 lines (34 loc) 1.27 kB
// change require("../index.js") to require("daas-sdk") // if you are using this code outside of this example folder const { DaasIoT, DriverTypes } = require("../index.js"); const daasApi = new DaasIoT("client"); const SID = 100; const CLIENT_DIN = 102; daasApi.doInit(SID, CLIENT_DIN); // Event when a new DIN is connected daasApi.onDinConnected((din) => { console.log("DIN Accepted:", din); }); const CLIENT_URL = '127.0.0.1:2102' // Enable the IPv4 driver on the client if (daasApi.enableDriver(DriverTypes.INET4, CLIENT_URL)) { console.log("Driver enabled!"); } // Mapping the Server DIN so that the client can send messages to it const SERVER_DIN = 101; const SERVER_URL = '127.0.0.1:2101' if (daasApi.map(SERVER_DIN, DriverTypes.INET4, SERVER_URL)) { console.log("Remote Node Mapped"); } // Activate the server if (daasApi.doPerform()) { console.log("Node performed!"); } // Send a message to the server (every 5 seconds) setInterval(() => { // Locate the server console.log("locate:", daasApi.locate(SERVER_DIN)) // Push data to the server daasApi.push(SERVER_DIN, 10, 0, JSON.stringify("Ciao Mondo!!!")); console.log(`⬆ Pushing data to ${SERVER_DIN} done.`); }, 5_000)