-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (79 loc) · 2.9 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
const tempGenerator = require('./helper/tempGenerator');
const warning = require('./warning/warning')
const deviceModule = require('aws-iot-device-sdk').device;
const fs = require('fs');
const path = require('path')
const device = deviceModule({
keyPath: "./cert/tempSensor.private.key",
certPath: "./cert/tempSensor.cert.pem",
caPath: "./cert/root-CA.crt",
clientId: "sdk-nodejs-b04d7c3d-f5b5-43cd-84cf-18e1b8c175b0",
host: "a1mrb3557hklej-ats.iot.us-west-2.amazonaws.com",
});
var timeout;
let intervalValue = JSON.parse(fs.readFileSync((path.resolve(__dirname, 'interval.json')))).interval
device.publish('getInterval', JSON.stringify({
interval: intervalValue
}));
timeout = setInterval(function() {
device.publish('getTemp', JSON.stringify({
temp: tempGenerator()
}));
}, intervalValue*60000); // clip to minimum
//
// Do a simple publish/subscribe demo based on the test-mode passed
// in the command line arguments. If test-mode is 1, subscribe to
// 'topic_1' and publish to 'topic_2'; otherwise vice versa. Publish
// a message every four seconds.to
//
device
.on('connect', function() {
device.subscribe('getTemp');
device.subscribe('needCurrentTemp');
device.subscribe('setInterval');
console.log('connect');
});
device
.on('close', function() {
console.log('close');
});
device
.on('reconnect', function() {
console.log('reconnect');
});
device
.on('offline', function() {
console.log('offline');
});
device
.on('error', function(error) {
console.log('error', error);
});
device
.on('message', function(topic, payload) {
console.info('topic', topic)
if (topic === 'getTemp') {
warning.checkTempFun(parseFloat(JSON.parse(payload).temp));
console.log('message', topic, payload.toString());
}
if (topic === 'needCurrentTemp') {
device.publish('currentTemp', JSON.stringify({
temp: tempGenerator()
}));
}
if (topic === 'setInterval') {
clearInterval(timeout)
let interval = parseFloat(JSON.parse(payload).message) * 60000
console.info('hello', parseFloat(JSON.parse(payload).message) * 60000)
device.publish('getTemp', JSON.stringify({
temp: tempGenerator()}));
timeout = setInterval(function() {
device.publish('getTemp', JSON.stringify({
temp: tempGenerator()}));
}, interval);
let intervalJson = {
interval: parseFloat(JSON.parse(payload).message)
}
fs.writeFileSync(path.resolve(__dirname, 'interval.json'), JSON.stringify(intervalJson))
}
});