Hi,
In extending the nodejs simulater device: https://mdash.net/docs/#nodejs-simulator
I need to send notification and send DB.Store commands through the websocket. I searched the forum and the docs but i am unable to get it working.
Here is a sample of what i have so far:
import Websocket from "ws";
import fetch from 'node-fetch';
import chalk from "chalk";
let connRPC = null;
const RPC = 'wss://mdash.net/api/v2/rpc?access_token=' + accessToken;
connRPC = new Websocket(RPC, {origin: RPC});
connRPC.on('message', msg => {
let req = JSON.parse(msg), resp = {}; // request and response RPC frames
if (req.method === 'Sys.GetInfo') {
resp = {id: req.id, result: {arch: 'nodejs', app: 'app1'}};
} else if (req.method === 'RPC.Describe') {
resp = {id: req.id, result: null};
} else if (req.method === 'RPC.List') {
resp = {id: req.id, result: ['Sys.GetInfo', 'RPC.List', 'RPC.Describe', 'RPC.Command']};
} else if (req.method === 'RPC.Command') {
resp = {id: req.id, result: {success: true}};
} else {
resp = {id: req.id, error: {code: -32601, message: 'method not found'}};
}
console.log(chalk.magenta('RPC:'), JSON.stringify(req), '->', JSON.stringify(resp));
connRPC.send(JSON.stringify(resp)); // Send reply to mDash
});
connRPC.on('error', error => {
console.error(chalk.red('Websocket error'), error);
})
setInterval(() => {
const msg = {
name: "rpc.out.MyStat", // "rpc.out.EVENT_NAME", in our case rpc.out.MyStat
data: {temperature: 12.34} // Your event payload - arbitrary
};
// This is not working. Nothing is saved in the database i mdash. I have tried to use the DB.Store as well but with any luck.
connRPC.send(JSON.stringify(msg));
console.log(chalk.bgYellow(chalk.black('Send sensor readings')), msg);
}, 5000)
Can anyone give a pointer? I think it is related to the json format i am sending, but the documentation does not help formatting raw messages - only how to use the mongoose os methods.
Thanks in advance