-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt-msgpack.js
51 lines (43 loc) · 1.16 KB
/
mqtt-msgpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var mqtt = require('mqtt');
var msgpack = require("msgpack-lite");
var client = mqtt.connect("mqtt://localhost");
var mongoose = require("mongoose");
var sleep = require('sleep');
var uuid = require('uuid');
mongoose.connect('mongodb://localhost/test');
var counter = 0
var time = 0;
var poSchema = mongoose.Schema({
date: String,
id: {type: String, unique: true},
lineItem: Number,
quantity: Number,
poNumber: String
});
var Purchase = mongoose.model('PurchaseOrder', poSchema);
var uuid = require('uuid');
client.on('connect', function () {
client.subscribe('/purchase', {qos:0});
});
// client.on('message', function (t,p) {});
client.handleMessage = function (packet, callback) {
// payload is msgpack buffer
if (packet.cmd == "publish") {
if (counter == 0) {
time = new Date().getTime();
}
counter++;
if (counter>=10000) {
nw = new Date().getTime();
delta = nw - time;
console.log(10000000/delta);
counter = 0;
}
var t = msgpack.decode(packet.payload);
t.id = uuid.v4()
po = new Purchase(t);
//console.log(msgpack.decode(packet.payload));
po.save();
}
callback();
};