Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI to facilitate the development #40

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tools/cli/cmds/listenCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-console */
const yargs = require('yargs');
const amqpConnection = require('./../network/connection').connection;

const onMessage = (channel, topic, key, callback) => {
const { queue } = channel.assertQueue(`${topic}-messages`, { durable: true });
channel.bindQueue(queue, topic, key);
return channel.consume(queue, callback);
};

const parseBuffer = buffer => JSON.parse(buffer.toString('utf-8'));

const listenCommands = (args) => {
const topic = 'fog';
const commands = ['data.update', 'data.request'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add device.* and schema.updated to get a response from the commands.


amqpConnection(args.hostname, args.port, topic, (connection, channel) => {
commands.forEach((cmd) => {
onMessage(channel, topic, cmd, (message) => {
const { content, fields } = message;
console.log(`Command received: ${fields.routingKey}`);
console.log(JSON.stringify(parseBuffer(content)));
channel.ack(message);
});
});
});
};

yargs
.command({
command: 'listen-commands',
desc: 'Listen to commands from cloud',
handler: (args) => {
listenCommands(args);
},
});