-
Notifications
You must be signed in to change notification settings - Fork 1
/
IrrigatorMQTT.js
executable file
·148 lines (123 loc) · 5.01 KB
/
IrrigatorMQTT.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
'use strict'
const {createServer} = require('http')
const {parse} = require('url')
const url = require('url')
const weather='/home'
const headers={"Content-Type" : "application/json"}
var mqtt = require('mqtt');
var settings = {
port: 1883
};
let s=createServer(
(req,res) => {
if(req.method === 'GET'){
if(req.url.toString() === '/irrigator002/start?' || req.url.toString() === '/irrigator002/start'){
let state = 'start'
let pressure = 35
let message = {
"state": state,
"pressure": pressure
}
let mex = `s|${state}|p|${pressure}`
console.log('Irrigator send state and pressure...');
// the client publish a new message
console.log(JSON.stringify(message))
client.publish('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/attrs', mex);
client.publish('/db/irrigator', JSON.stringify(message))
setTimeout(stopping, 30000)
}
if(req.url.toString() === '/irrigator002/slow?' || req.url.toString() === '/irrigator002/slow'){
let state = 'slow'
let pressure = 20
let message = {
"state": state,
"pressure": pressure
}
let mex = `s|${state}|p|${pressure}`
console.log('Irrigator send state and pressure...');
// the client publish a new message
console.log(JSON.stringify(message))
client.publish('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/attrs', mex);
client.publish('/db/irrigator', JSON.stringify(message))
setTimeout(stopping, 45000)
}
if(req.url.toString() === '/irrigator002/fast?' || req.url.toString() === '/irrigator002/fast'){
let state = 'fast'
let pressure = 45
let message = {
"state": state,
"pressure": pressure
}
let mex = `s|${state}|p|${pressure}`
console.log('Irrigator send state and pressure...');
// the client publish a new message
console.log(JSON.stringify(message))
client.publish('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/attrs', mex);
client.publish('/db/irrigator', JSON.stringify(message))
setTimeout(stopping, 15000)
}
if(req.url.toString() === '/irrigator002/stop?' || req.url.toString() === '/irrigator002/stop'){
let state = 'stop'
let pressure = 0
let message = {
"state": state,
"pressure": pressure
}
let mex = `s|${state}|p|${pressure}`
console.log('Irrigator send state and pressure...');
// the client publish a new message
client.publish('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/attrs', mex);
client.publish('/db/irrigator', JSON.stringify(message))
console.log(JSON.stringify(message))
console.log("Stopping Irrigator");
setTimeout(stop, 3000)
}
}
})
s.listen(8080)
var client = mqtt.connect('mqtt://127.0.0.1', settings);
// the client subscribe some new topic
client.subscribe('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/message');
console.log('Irrigator listen for a command...');
// fired when new message is received
function stopping(){
let state = 'stop'
let pressure = 0
let message = {
"state": state,
"pressure": pressure
}
let mex = `s|${state}|p|${pressure}`
console.log(JSON.stringify(message))
// client.publish('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/message', info);
client.publish('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/attrs', mex);
console.log("Stopping Irrigator");
setTimeout(stop, 1000)
}
function stop(){
process.exit()
}
client.on('message', function(topic, message) {
//console.log(JSON.parse(message.toString()))
let obj = message.toString().split('|');
let state = obj[1]
let pressure = 0
if(state === 'start'){
pressure = 35
}else if (state === 'slow'){
pressure = 20
}else if (state === 'fast'){
pressure = 45
}else if(state === 'stop'){
pressure = 0
}
message = {
"state": state,
"pressure": pressure
}
let mex = `s|${state}|p|${pressure}`
console.log('Irrigator send state and pressure...');
// the client publish a new message
console.log(JSON.stringify(message))
client.publish('/ul/4jggokgpepnvsb2uv4s40d59ov/irrigator002/attrs', mex);
});