-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.js
80 lines (62 loc) · 1.43 KB
/
mock.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
'use strict';
var request = require('request');
var mqtt = require('mqtt');
var interval = 1000;
// function getRandomInt(min, max) {
// return Math.floor(Math.random() * (max - min + 1)) + min;
// }
var client = mqtt.connect({
servers: [{
'host': 'mqtt.relayr.io',
'port': 8883
}],
username: 'd4b7cc40-e399-4367-8ab6-90f165a98315',
password: 'EP6d.Q9CD9h5',
clientId: 'GotMilk!',
protocol: 'mqtts',
certPath: __dirname + '/lib/relayr.crt',
rejectUnauthorized: false
});
client.subscribe('/v1/73927f8d-5135-40d0-9aa9-3837cee5e1e3');
client.on('message', function(topic, msg) {
msg = JSON.parse(msg);
if (!msg.weight)
return;
var data = {
device: 'scale',
// weight: getRandomInt(0, 1000),
weight: Math.round(msg.weight),
};
console.log(data);
request({
uri : 'http://gilles.local:3001/consumer',
method : 'POST',
body : data,
json : true,
});
});
// send fridge closed event
// setTimeout(closeFridge, 2000);
// setTimeout(openFridge, 4000);
function closeFridge() {
request({
uri : 'http://gilles.local:3001/consumer',
method : 'POST',
body : {
device: 'light_sensor',
luminosity: 90
},
json : true,
});
}
function openFridge() {
request({
uri : 'http://gilles.local:3001/consumer',
method : 'POST',
body : {
device: 'light_sensor',
luminosity: 300
},
json : true,
});
}