Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local ip address #9

Merged
merged 5 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
{
"name": "MP1",
"type": "MP",
"mac": "CA:32:4F:77:D2:21"
"mac": "CA:32:4F:77:D2:21",
"local_ip_address": "192.168.0.1"
}
]
}]
Expand Down
30 changes: 18 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function BroadlinkAccessory(log, config) {
this.ip = config.ip;
this.mac = config.mac;
this.powered = false;
this.local_ip_address = config.local_ip_address;

if (!this.ip && !this.mac) throw new Error("You must provide a config value for 'ip' or 'mac'.");

Expand Down Expand Up @@ -111,10 +112,15 @@ BroadlinkAccessory.prototype = {
return services;
},

// b: broadlink
discover: function(b) {
b.discover(this.local_ip_address);
},

getSPState: function(callback) {
var self = this;
var b = new broadlink();
b.discover();
self.discover(b);

b.on("deviceReady", (dev) => {
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
Expand All @@ -136,15 +142,15 @@ BroadlinkAccessory.prototype = {
}
});
var checkAgainSP = setInterval(function() {
b.discover();
self.discover(b);
}, 1000)

},

setSPState: function(state, callback) {
var self = this;
var b = new broadlink();
b.discover();
self.discover(b);

self.log("set SP state: " + state);
if (state) {
Expand All @@ -164,7 +170,7 @@ BroadlinkAccessory.prototype = {
}
});
var checkAgainSPset = setInterval(function() {
b.discover();
self.discover(b);
}, 1000)
}
} else {
Expand All @@ -182,7 +188,7 @@ BroadlinkAccessory.prototype = {
}
});
var checkAgainSPset = setInterval(function() {
b.discover();
self.discover(b);
}, 1000)
} else {
return callback(null, false)
Expand All @@ -195,7 +201,7 @@ BroadlinkAccessory.prototype = {
var b = new broadlink();
var s_index = self.sname[1];
self.log("checking status for " + self.name);
b.discover();
self.discover(b);
b.on("deviceReady", (dev) => {
//self.log("detected device type:" + dev.type + " @ " + dev.host.address);
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
Expand Down Expand Up @@ -223,14 +229,14 @@ BroadlinkAccessory.prototype = {
});
var checkAgain = setInterval(function() {
//self.log("Discovering Again for Status... " + self.sname);
b.discover();
self.discover(b);
}, 1000)


},

setMPstate: function(state, callback) {
var self = this
var self = this;
var s_index = self.sname[1];
var b = new broadlink();

Expand All @@ -239,7 +245,7 @@ BroadlinkAccessory.prototype = {
if (self.powered) {
return callback(null, true);
} else {
b.discover();
self.discover(b);
b.on("deviceReady", (dev) => {
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
self.log(self.sname + " is ON!");
Expand All @@ -254,12 +260,12 @@ BroadlinkAccessory.prototype = {
});
var checkAgainSet = setInterval(function() {
//self.log("Discovering Again for Set Command... " + self.sname);
b.discover();
self.discover(b);
}, 1000)
}
} else {
if (self.powered) {
b.discover();
self.discover(b);
b.on("deviceReady", (dev) => {
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
self.log(self.sname + " is OFF!");
Expand All @@ -274,7 +280,7 @@ BroadlinkAccessory.prototype = {
});
var checkAgainSet = setInterval(function() {
//self.log("Discovering Again for Set Command... " + self.sname);
b.discover();
self.discover(b);
}, 1000)
} else {
return callback(null, false)
Expand Down