-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent3TDAgent.js
More file actions
107 lines (86 loc) · 2.76 KB
/
agent3TDAgent.js
File metadata and controls
107 lines (86 loc) · 2.76 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Kyle Bowerman 1/9/2015
the agent will post temp readings to Treasure Data.
*/
var request = require('request');
var moment = require('moment');
var moistureA1 = 0;
var lightA0 = 0;
var tdAgentURL = process.env.TD_AGENT_URL;
function postToTDAgent(payload) {
//Make the post
console.log(' called the post');
request({
method: 'POST',
url: tdAgentURL,
body: payload,
json: true
}, function(err, rsp,body){
if (err || rsp.statusCode != 200) { console.log('HTTP error:', err, rsp.statusCode, body); }
else {
console.log('Got HTTP Response code %d', 200);
}
});
}
function getReading(url, msg, pin, device, err, rsp, callback) {
//Make the request
request(url, function (error, response, body) {
if(msg) { console.log(msg); }
if (!error && response.statusCode == 200) {
console.log(body);
var parsebody = body.split(" ");
pinValue = Number(parsebody[4]);
console.log('The Value of ' + pin + ' pin is ', pinValue);
//var d = new Date();
var currentTime = moment();
var epochtime = moment().valueOf();
var payload = {
msg: 'device',
body: body,
pin: pin,
device: device,
version: 2,
class: 'analog',
value: pinValue,
time: currentTime,
devicetime: currentTime,
epochtime: epochtime
};
postToTDAgent(payload);
}
});
}
function getTempReading(url, msg, pin, device, err, rsp, callback) {
//Make the request
request(url, function (error, response, body) {
if(msg) { console.log(msg); }
if (error ) { console.log('there is an error ' + error );}
if (!error && response.statusCode == 200) {
jsonBody = JSON.parse(body);
//var d = new Date();
var currentTime = moment();
var epochtime = moment().valueOf();
var payload = {
msg: device,
body: jsonBody,
pin: pin,
device: jsonBody.device,
version: 2,
class: 'temp',
value: jsonBody.temp,
time: currentTime,
devicetime: currentTime,
epochtime: epochtime
};
postToTDAgent(payload);
}
});
}
// Make the call to get moisture
getReading('http://localhost/arduino/analog/1/', ' getting the moisture reading from A1', 'A1', 'moisture');
// Make the call to get light
getReading('http://localhost/arduino/analog/0/', ' getting the light reading from A0', 'A0', 'light');
// Make the call to get T1
getTempReading('http://localhost/arduino/temp/1', 'getting the reading from T1', 'T1', 'temp at window');
// Make the call to get T1
getTempReading('http://localhost/arduino/temp/2', 'getting the reading from T2', 'T2', 'temp inside');
//getReading('http://localhost/arduino/analog/2/');