mqtt added
This commit is contained in:
parent
d1bb5d07e8
commit
b6e34999b1
@ -12,6 +12,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/commander": "^2.9.0",
|
"@types/commander": "^2.9.0",
|
||||||
|
"@types/mqtt": "0.0.34",
|
||||||
"@types/node": "^7.0.14",
|
"@types/node": "^7.0.14",
|
||||||
"typescript": "^2.3.1"
|
"typescript": "^2.3.1"
|
||||||
},
|
},
|
||||||
|
30
src/main.ts
30
src/main.ts
@ -1,18 +1,40 @@
|
|||||||
|
import * as Mqtt from 'mqtt'
|
||||||
|
|
||||||
class MqttMongo {
|
class MqttMongo {
|
||||||
|
private mqttClient : Mqtt.Client
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
exec(): void {
|
exec(): void {
|
||||||
console.log("Hello, %s", program['name'])
|
this.mqttClient = Mqtt.connect(options['broker'])
|
||||||
|
this.mqttClient.on('connect', () => {
|
||||||
|
console.log("connected to broker")
|
||||||
|
this.mqttClient.subscribe('MqttMongo/Command')
|
||||||
|
this.mqttClient.publish('MqttMongo/Status', 'hello, started up')
|
||||||
|
})
|
||||||
|
this.mqttClient.on('message', (topic : string, message : string) => {
|
||||||
|
console.log("message received")
|
||||||
|
console.log("topic %s", topic)
|
||||||
|
console.log("message %s", message)
|
||||||
|
|
||||||
|
if (topic == "MqttMongo/Command" && message == "shutdown") {
|
||||||
|
this.mqttClient.end()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.mqttClient.on('close', () => {
|
||||||
|
console.log("bye bye")
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log("Hello world")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
import program = require('commander')
|
import options = require('commander')
|
||||||
program
|
options
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
.option('-n, --name [name]', 'Name to greet', 'Wolfgang')
|
.option('-b, --broker [broker url]', 'Broker URL', 'mqtt://mqttbroker')
|
||||||
.parse(process.argv)
|
.parse(process.argv)
|
||||||
|
|
||||||
const mqttMongo = new MqttMongo()
|
const mqttMongo = new MqttMongo()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user