-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (35 loc) · 805 Bytes
/
server.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
var mdns = require('mdns-js')
var axios = require('axios')
var browser = mdns.createBrowser(mdns.tcp('http'))
let sonoff = {}
browser.on('ready', function () {
browser.discover()
})
browser.on('update', function (data) {
if (data.host.includes('sonoff')) {
sonoff.ip = data.addresses
sonoff.host = data.host
console.log(sonoff)
toggle()
}
})
function toggle () {
axios.get(`http://${sonoff.ip}/cm?cmnd=Power%20TOGGLE`)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
}
function power (query) {
// QUery must be on/off
axios.get(`http://${sonoff.ip}/cm?cmnd=Power%20${query}`)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
}
function