-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.js
64 lines (55 loc) · 1.86 KB
/
insert.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
52
53
54
55
56
57
58
59
60
61
62
63
64
const mqtt = require ('mqtt');
const fs = require('fs');
var WebSocketServer = require("ws").Server;
const https = require('https');
const server = new https.createServer({
cert: fs.readFileSync('/etc/lego/certificates/www.mqtest.cf.crt'),
key: fs.readFileSync('/etc/lego/certificates/www.mqtest.cf.key')
});
var wss = new WebSocketServer({server});
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://user1:arijeet1@ds231991.mlab.com:31991/machinedetails";
var client = mqtt.connect('1883:www.mqtest.cf');
client.on('connect', function () {
client.subscribe('hello/cat');
console.log('client has subscribed successfully');
});
client.on('message',function(topic,message) {
var d = new Date();
kat = message.toString();
sat = JSON.parse(kat);
sat["year"] = d.getFullYear();
sat["month"] = d.getMonth()+1;
sat["date"] = d.getDate();
sat["hour"] = d.getHours();
sat["minutes"] = d.getMinutes();
sat["seconds"] = d.getSeconds();
sat["milliseconds"] = d.getMilliseconds();
sat["day"] = d.getDay();
sat["totmili"] = d.getTime();
//sat["info"] = d;
// sat["info"] = d;
nat = JSON.stringify(sat);
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("machinedetails");
//var myobj = { name: "Company Inc", address: "Highway 37" };
dbo.collection("esp").insertOne(sat, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
//var offset = (new Date().getTimezoneOffset() / 60) * -1;
//console.log(offset);
// console.log(d);
console.log(nat);
wss.clients.forEach(function(client) {
client.send(nat);
});
//client.end();
});
wss.on("connection",function(ws) {
ws.send("Welcome Tega Broker");
});
server.listen(3000);