-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
231 lines (202 loc) · 6.77 KB
/
index.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"use strict";
// Dependencies
var awsIot = require('aws-iot-device-sdk');
var CronJob = require('cron').CronJob;
var request = require('request');
var SerialPort = require('serialport');
var winston = require('winston');
const Readline = require('@serialport/parser-readline');
// Device Data
// format is [{"hour": 12, "minute": 40, "duration": 20}]; //starttime mins
var pumpData = [];
var pumpCrons = [];
var dataCron = null;
var sendDataInterval; // in hours
// Logs
const logger = winston.createLogger({
levels: {
pump: 0,
sentData: 1,
pumpCron: 2,
awsError: 3,
sentCron: 4,
recvData: 5
},
format: winston.format.json(),
defaultMeta: { service: 'sp-service' },
transports: [
// - Write to all logs with level `info` and below to `combined.log`
new winston.transports.File({ filename: 'combined.log', level: 'recvData' })
]
});
// Topics
var receiveTopic = 'topic_1';
var sendTopic = 'topic_2';
// AWS device data
var URL_SERVICE = 'https://qtscqc0ln4.execute-api.us-east-1.amazonaws.com/dev/device/param/';
var device = awsIot.device({
keyPath: "./certs/testYun.private.key",
certPath: "./certs/testYun.cert.pem",
caPath: "./certs/root-CA.crt",
clientId: "basicPubSub",
host: "a199i8mfkofeen-ats.iot.us-east-1.amazonaws.com"
});
// Serial port over USB connection between the Raspberry Pi and the Arduino
var arduinoSerialPort = '/dev/ttyACM0';
// Setting data to SerialPort library
const port = new SerialPort(arduinoSerialPort, { baudRate: 9600 });
const parser = new Readline();
port.pipe(parser);
// Start data
defaultParams() // set Params by default
createCronData(); // create cron for send data to aws (see interval param) with default params
createCronPump(); // create cron for pump with default params
//
// Device is an instance returned by mqtt.Client(), see mqtt.js for full
// documentation.
//
device
.on('connect', function() {
console.log('connect');
device.subscribe(receiveTopic);
updatePumpCrons(); // when internet up, update crons for pump
updateSampleInterval(); // // when internet up, update sampleInterval
parser
.on('data', function (data) {
//When a new line of text is received from Arduino over Serial
try {
console.log(data);
var toSend = JSON.stringify(JSON.parse(data)); // validate received JSON
//Forward the Arduino information to AWS IoT
device.publish(sendTopic, toSend);
logger.log('sentData', {"date": new Date(), "data": toSend}); // write to log
}
catch (err) {
console.warn(err);
logger.log('awsError', {"date": new Date(), "data": String(err)}); // write to log
}
});
});
device
.on('message', function(topic, payload) {
console.log('Recv message on ', topic, ': ', payload.toString());
var payloadJson = JSON.parse(payload);
// If message is 'updPumpSched' then update crons for pump
if(payloadJson.message == 'updPumpSched') {
updatePumpCrons();
};
if(payloadJson.message == 'updSampleInterval') {
updateSampleInterval();
};
if(payloadJson.message == 'resetArduino') {
resetArduino();
};
logger.log('recvData', {"date": new Date(), "message": payloadJson});
port.write(payloadJson.message + '\n'); // Send data to Arduino over Serial
});
device
.on('close', function() {
console.log('close');
});
device
.on('reconnect', function() {
console.log('reconnect');
});
device
.on('offline', function() {
console.log('offline');
logger.log('awsError', {"date": new Date(), "data": 'Offline'});
});
device
.on('error', function(error) {
console.log('error', error);
logger.log('awsError', {"date": new Date(), "data": String(error)});
});
// Sets pumpData by defaults, so if not internet it runs by default
function defaultParams() {
sendDataInterval = 6; // each 6 hours
pumpData = [
{
"duration": 60,
"hour": 23,
"minute": 0
},
{
"duration": 60,
"hour": 12,
"minute": 10
}
];
}
// Get cronsDataPump from Api and create crons
function updatePumpCrons() {
pumpData = []; // clear array
const url = URL_SERVICE + 'get?topic=' + receiveTopic + '¶m=pumpSched';
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recvData = JSON.parse(body);
pumpData = recvData[0].data
logger.log('pump', {"date": new Date(), "data": pumpData }); // write to log
clearCronPump(); // clear cronsPump
createCronPump(); // call cronPump creation
}
});
}
// Create crons for Pump from pumpData array
function createCronPump() {
// Create array of crons
pumpData.forEach((job, i) => { // for each cron in pumpData
var time = pumpData[i].minute + ' ' + pumpData[i].hour + ' * * *';
// Create a new CronJob
pumpCrons[i] = new CronJob(time, () => {
console.log('Running Job: ' + JSON.stringify(job));
var ardComm = 'm ' + pumpData[i].duration + '\n'; // command to send to Arduino
port.write(ardComm); // Send data to Arduino over Serial
logger.log('pumpCron', {"date": new Date(), "data": ardComm }); // write to log
});
pumpCrons[i].start();
});
}
// Clear and Stop all Pump Cron jobs
function clearCronPump() {
pumpCrons.forEach((job,i) => {
pumpCrons[i].stop();
});
pumpCrons = [];
}
// Get data (ph,temp,orp) from Arduino stoppig Pump and send to Aws
function getDataFromArduino() {
var ardComm = 'g' + '\n'; // command to send to Arduino
port.write(ardComm); // Send data to Arduino over Serial
}
// Get sendDataInterval (SampleInterval) from Api
function updateSampleInterval() {
const url = URL_SERVICE + 'get?topic=' + receiveTopic + '¶m=sampleInterval';
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recvData = JSON.parse(body);
sendDataInterval = recvData[0].data; // update data
logger.log('sentCron', {"date": new Date(), "data": 'SampleInterval updated: '+ String(sendDataInterval) }); // write to log
// if cron exists, then delete
if (dataCron !== null) {
dataCron.stop();
dataCron = null;
}
createCronData(); // call cronData creation
}
});
}
// Create cron for get data (ph,temp,orp) from Arduino and send to Aws
function createCronData() {
var time = '0 */'+ sendDataInterval +' * * *';
// Create a new CronJob
dataCron = new CronJob(time, () => {
getDataFromArduino();
logger.log('sentCronLog', {"date": new Date(), "message": "Running Cron to get Data" }); // write to log
});
}
// Reset Arduino
function resetArduino() {
var ardComm = 'r' + '\n'; // command to send to Arduino
port.write(ardComm); // Send data to Arduino over Serial
}