introduce mongodb connection
This commit is contained in:
parent
3b25d3df89
commit
a421153173
@ -11,17 +11,19 @@
|
|||||||
"author": "Wolfgang Hottgenroth",
|
"author": "Wolfgang Hottgenroth",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type" : "git",
|
"type": "git",
|
||||||
"url" : "git@gitlab.com:wolutator/MqttMongoNodejs.git"
|
"url": "git@gitlab.com:wolutator/MqttMongoNodejs.git"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/commander": "^2.9.0",
|
"@types/commander": "^2.9.0",
|
||||||
|
"@types/mongodb": "^2.2.0",
|
||||||
"@types/mqtt": "0.0.34",
|
"@types/mqtt": "0.0.34",
|
||||||
"@types/node": "^7.0.14",
|
"@types/node": "^7.0.14",
|
||||||
"typescript": "^2.3.1"
|
"typescript": "^2.3.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"commander": "^2.9.0",
|
"commander": "^2.9.0",
|
||||||
|
"mongodb": "^2.2.26",
|
||||||
"mqtt": "^2.6.2"
|
"mqtt": "^2.6.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
src/main.ts
26
src/main.ts
@ -1,13 +1,31 @@
|
|||||||
import * as Mqtt from 'mqtt'
|
import * as Mqtt from 'mqtt'
|
||||||
|
import * as Mongo from 'mongodb'
|
||||||
|
|
||||||
|
|
||||||
|
var MQTT_BROKER_URL : String = 'mqtt://localhost'
|
||||||
|
var MONGO_DATABASE_URL : String = 'mongodb://localhost/test'
|
||||||
|
|
||||||
|
|
||||||
class MqttMongo {
|
class MqttMongo {
|
||||||
private mqttClient : Mqtt.Client
|
private mqttClient : Mqtt.Client
|
||||||
|
private dbHandle : Mongo.Db;
|
||||||
|
private msgCnt : number = 0;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.mqttClient = Mqtt.connect(options['broker'])
|
this.mqttClient = Mqtt.connect(options['broker'])
|
||||||
this.mqttClient.on('offline', () => { console.log("mqtt client is offline") })
|
this.mqttClient.on('offline', () => { console.log("mqtt client is offline") })
|
||||||
this.mqttClient.on('reconnect', () => { console.log("mqtt client is reconnecting") })
|
this.mqttClient.on('reconnect', () => { console.log("mqtt client is reconnecting") })
|
||||||
this.mqttClient.on('close', () => { console.log("mqtt connection closed") })
|
this.mqttClient.on('close', () => { console.log("mqtt connection closed") })
|
||||||
|
|
||||||
|
/*
|
||||||
|
Mongo.MongoClient.connect(options['database'])
|
||||||
|
.then(
|
||||||
|
(dbHandle: Mongo.Db) => { this.dbHandle = dbHandle },
|
||||||
|
(err: String) => {
|
||||||
|
console.log("Unable to connect to database: %s", err)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
exec(): void {
|
exec(): void {
|
||||||
@ -18,9 +36,8 @@ class MqttMongo {
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.mqttClient.on('message', (topic : string, message : string) => {
|
this.mqttClient.on('message', (topic : string, message : string) => {
|
||||||
console.log("message received")
|
this.msgCnt++;
|
||||||
console.log("topic %s", topic)
|
console.log(`message received ${this.msgCnt}, topic ${topic}, payload ${message}`)
|
||||||
console.log("message %s", message)
|
|
||||||
|
|
||||||
if (topic == "MqttMongo/Command" && message == "shutdown") {
|
if (topic == "MqttMongo/Command" && message == "shutdown") {
|
||||||
this.mqttClient.end()
|
this.mqttClient.end()
|
||||||
@ -33,7 +50,8 @@ class MqttMongo {
|
|||||||
import options = require('commander')
|
import options = require('commander')
|
||||||
options
|
options
|
||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
.option('-b, --broker [broker url]', 'Broker URL', 'mqtt://mqttbroker')
|
.option('-b, --broker [broker url]', 'Broker URL', MQTT_BROKER_URL)
|
||||||
|
.option('-m, --database [database url]', 'MongoDB Database URL', MONGO_DATABASE_URL)
|
||||||
.parse(process.argv)
|
.parse(process.argv)
|
||||||
|
|
||||||
const mqttMongo = new MqttMongo()
|
const mqttMongo = new MqttMongo()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user