Skip to content

Commit 85014db

Browse files
committed
Historise all messages in mongodb
1 parent 1982642 commit 85014db

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"password": "mosquitto password"
99
}
1010
},
11+
"mongo":
12+
{
13+
"host": "mongodb://localhost:27017/home"
14+
},
1115
"redis":
1216
{
1317
"host": "localhost",

homemon-stats/homemon-stats.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ mqttclient.on('connect', function() {
183183
});
184184

185185

186+
var MongoClient = require ("mongodb").MongoClient;
187+
188+
186189

187190
var mqtt2 = require('mqtt');
188191
var mqttclient2 = mqtt2.connect(config.mqtt.host, config.mqtt.options);
@@ -198,7 +201,14 @@ mqttclient2.on('connect', function() {
198201
value: Number(message.toString()),
199202
timeStamp: Number(time)
200203
};
204+
201205
mqttclient2.publish("jsonsensors", JSON.stringify(jsonMessage));
206+
207+
208+
MongoClient.connect (config.mongo.host, function (err, db) {
209+
db.collection ('homedata').insertOne (jsonMessage);
210+
db.close ();
211+
});
202212
});
203213
});
204214

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"sync-exec": "*",
1212
"socket.io": "*",
1313
"redis": "*",
14-
"mqtt": "*"
14+
"mqtt": "*",
15+
"mongodb": "*"
1516
}
16-
}
17+
}

0 commit comments

Comments
 (0)