sensordata-websocket
v1.2.8
Published
This package allows for easy websocket connection to sensordata.space, where data can be stored for various graphical representations.
Readme
Sensordata-WebSocket
This package allows for easy websocket connection to sensordata.space, where data can be stored for various graphical representations.
The devices can also receive commands and event changes from the server, which can be used to control the devices.
How to use it
Installation
npm install sensordata-websocketInitialization
import SocketClient from 'sensordata-websocket';
or
const SocketClient = require('sensordata-websocket');Create a new instance of the SocketClient class
const sensordataClient = new SocketClient();Declare communication functions
sendStatus()
This function specifies the status that is to be sent to the server.
The status is a flat json object of numbers | strings | booleans.
It is recommended that the status is a global variable, so that it can be easily modified.Each property of the status object will represent an entity of the device.
let status = {
count: 0,
led: false,
}
function sendStatus() {
// here you can modify the status object
// and then return it
return status;
}receivedCommand()
This function is called when a command is received from the server.
The command provided is a string value.
function receivedCommand(command) {
console.log(`Received command: ${command}`);
if (command === "increment") {
status.count++;
} else if (command === "decrement") {
status.count--;
}
}entityChanged()
This function is called when an entity is changed on the server.
The device is informed of the new value of the entity.
function entityChanged(entity, value) {
console.log(`Entity "${entity}" changed to ${value}`);
if (entity === "led") {
status.led = value;
}
}Connect to the server
Each instance you are running, represents a device on the server.
Each device has a mac-address that is automatically acquired from the sensordata-websocket library.
That means that you cannot run multiple instances on the same machine, because they will have the same mac-address.
Also, in order to authenticate the device, you need to provide a token that is provided by the server on your user-profile page.
The following code connects the communication functions to the sensordataClient instance, passing the device object, and then it connects to the server.
function main() {
const device = {
deviceApp: "Example JS device",
version: "1.0.0",
deviceType: "PC",
token: process.env.DEVICE_TOKEN,
}
sensordataClient.setSendStatusFunction(sendStatus);
sensordataClient.setReceivedCommandFunction(receivedCommand);
sensordataClient.setEntityChangedFunction(entityChanged);
sensordataClient.setDevice(device);
sensordataClient.init("api.sensordata.space", 443, true);
}
main();Send status to the server
Anytime you want to send the status to the server, you can call the sendStatusWithSocket method like so:
const saveToDB = false;
sensordataClient.sendStatusWithSocket(saveToDB);This method internnally calls the sendStatus function (created earlier), to get the current status and then it sends the status to the server.
sendNotification(message, data)
This function sends a notification to the server. According to the notification service that you have chosen, the notification will be sent to the user either with expo notifications or pushover.
const message = "This is a notification message";
const options = {
// ...pushover options (only if you are using pushover)
}
sensordataClient.sendNotification(message, options);sendLog(message)
This function sends a log message to the server. The log message will be saved in the database and can be viewed in the logs section of the device settings.
const message = "This is a log message";
sensordataClient.sendLog(message);Examples
Check out the examples folder for detailed usage scenarios:
- Basic Usage: basic.js
- Advanced Usage: advanced-usage.js
