-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll3r.js
60 lines (53 loc) · 1.68 KB
/
poll3r.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
require('dotenv').config();
var request = require('request');
var traverse = require('traverse');
var tag = require('./js/tag');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var reqForm = {
meterTag: tag.meterTag,
user: process.env.USERNAME_WEB,
password: process.env.PASSWORD_WEB
}
var dbURL = 'mongodb://poll3r:pol%406xJ@localhost:27017/pm5560';
var db;
var meterData = function(collection) {
var rxBuff = [];
request.post({
url: 'https://acbb50727ee6ae8780b9ac5276cfb5cf.resindevice.io',
form: reqForm },
function(error, response, body) {
if (error) console.log(error); // changed to avoid ECONHOST error
//console.log(response.statusCode);
if (response.statusCode == 200) {
rxBuff = body.split(",");
if (rxBuff.length > 10) { // If buff is empty, don't save to DB
var meterJson = tag.meterJson(rxBuff);
traverse(meterJson).forEach(function (x) {
if (x === "***") this.delete(stopHere=false);
});
traverse(meterJson).forEach(function (x) {
if (x == "" || (x < 0.1 && x > 0) ) this.delete(stopHere=false);
});
console.log(meterJson);
collection.insertOne(meterJson);
console.log("gravou no banco");
}
}
}
).on('error', function(e) { // added to avoid ECONHOST error
console.log(e);
});
}
MongoClient.connect(dbURL,{uri_decode_auth: true}, function(err, database) {
assert.equal(null, err);
db = database;
console.log("Connected correctly to server");
var hpCollection = db.collection('hp');
setInterval(
function () {
meterData(hpCollection);
},
60000
);
});