Skip to content

Commit

Permalink
Optional setting for bridge IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
EDelsman committed Apr 28, 2021
1 parent 27155df commit 97ccd40
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .homeychangelog.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"1.0.9": {
"en": "• Support for periodical state refresh (currently 4x a day) • Made driver more generic.",
"nl": "• Ondersteuning voor periodieke statusverversing (nu 4x per dag) • Driver generieker gemaakt."
"en": "• Support for periodical state refresh (currently 4x a day) • Made driver more generic • Optional setting for bridge IP address.",
"nl": "• Ondersteuning voor periodieke statusverversing (nu 4x per dag) • Driver generieker gemaakt • Optionele instelling voor bridge IP adres."
},
"1.0.8": {
"en": "• Fixed battery percentages.",
Expand Down
8 changes: 6 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MotionBlinds extends Homey.App {
this.mdriver.logHeartbeat = this.mdriver.verbose;
this.mdriver.multicast = this.homey.settings.get('multicast') == true;
let key = this.homey.settings.get('motion_key');
this.mdriver.setIP(this.homey.settings.get('motion_ip'));
if (key != null && key != undefined && key.length == 16)
try {
this.mdriver.setAppKey(key);
Expand All @@ -42,15 +43,18 @@ class MotionBlinds extends Homey.App {

async onClearSettings() {
this.mdriver.setAppKey(null);
this.mdriver.multicast = false;
this.mdriver.verbose = false;
this.mdriver.logHeartbeat = false;
this.mdriver.setIP(undefined);
}

onBlockAction(args, state) {
async onBlockAction(args, state) {
if (args.device != undefined)
args.device.onBlockAction(args, state);
}

onUnblockAction(args, state) {
async onUnblockAction(args, state) {
if (args.device != undefined)
args.device.onUnblockAction(args, state);
}
Expand Down
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"keyLocation3": "A valid key has the following format: 74ae544c-d16e-4c",
"keyField": "Key",
"options": "Options",
"ip": "Bridge IP address",
"debugDescription": "Only turn the settings below on if the developer asks you to do so:",
"debug": "Log all messages for debugging",
"debug": "Fully log all messages",
"multicast": "Only use multicast",
"save": "Save settings",
"clear": "Clear settings"
Expand Down
3 changes: 2 additions & 1 deletion locales/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"keyLocation3": "Een geldige sleutel heeft het volgende formaat: 74ae544c-d16e-4c",
"keyField": "Sleutel",
"options": "Opties",
"ip": "Bridge IP adres",
"debugDescription": "De onderstaande instellingen alleen aanzetten als de ontwikkelaar hier om vraagt:",
"debug": "Alle meldingen loggen voor het debuggen",
"debug": "Alle berichten volledig loggen",
"multicast": "Alleen multicast gebruiken",
"save": "Instellingen opslaan",
"clear": "Instellingen wissen"
Expand Down
56 changes: 40 additions & 16 deletions motion/motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Quickly tap the 'Motion APP about' 5 times to get the key, it should have format
You can listen to the following events (though there should not be any imminent need):
'listening' The UDP socket is now listening
'error' An error occurred
'senderror' An error occurred during send
'close' Cle UPD socket was closed
'connect' The Motion Gateway is being connected
'disconnect' The Motion Gateway is being disconnected
Expand Down Expand Up @@ -194,6 +194,8 @@ class MotionDriver extends EventEmitter {
this.pollAgain = false;
this.pollTimer = undefined;
this.multicast = false;
this.ip = undefined;
this.listening = false;
this.client = UDP.createSocket({ type: 'udp4', reuseAddr: true });
this.client.on('listening', function() { this.onListening() }.bind(this));
this.client.on('error', function(error) { this.onError(error) }.bind(this));
Expand Down Expand Up @@ -221,6 +223,12 @@ class MotionDriver extends EventEmitter {
return new Date().toISOString().replace(/[T\-\./:Z]/g, '');
}

setIP(ip) {
this.ip = ip == null || ip == '' ? undefined : ip;
if (this.listening)
this.send({ "msgType": "GetDeviceList", "msgID": this.getMessageID() });
}

setAppKey(appKey) {
if (appKey == undefined)
appKey = null;
Expand All @@ -246,37 +254,47 @@ class MotionDriver extends EventEmitter {
}

percentageClosedToPosition(perc) {
if (perc == undefined)
return undefined;
let pos = Math.round(perc * this.Position.Close_Down);
return Math.max(Math.min(pos, this.Position.Close_Down), this.Position.Open_Up);
}

percentageOpenToPosition(perc) {
return this.Position.Close_Down - this.percentageClosedToPosition(perc);
return perc == undefined ? undefined : this.Position.Close_Down - this.percentageClosedToPosition(perc);
}

positionToPercentageClosed(pos) {
if (pos == undefined)
return undefined;
let perc = Math.round(pos) / this.Position.Close_Down;
return Math.min(Math.max(perc, 0), 1);
}

positionToPercentageOpen(pos) {
return 1 - this.positionToPercentageClosed(pos);
return pos == undefined ? undefined : 1 - this.positionToPercentageClosed(pos);
}

angleToPercentageTilt(angle, max_tilt = this.Angle.Close) {
if (angle == undefined)
return undefined;
let perc = Math.round(angle) / max_tilt;
return 1 - Math.min(Math.max(perc, 0), 1);
}

percentageTiltToAngle(perc, max_tilt = this.Angle.Close) {
if (angle == undefined)
return undefined;
let angle = Math.round(perc * max_tilt);
return max_tilt - Math.max(Math.min(angle, max_tilt), this.Angle.Open);
}

batteryLevelToPercentage(level) {
if (level == undefined)
return undefined;
let voltage = level / 100;
let cells = Math.round(voltage / 3.7); // estimate nr of cells, min is 3.2 and max is 4.2 per cell, will work ok for about 2 to 4 cells
let min = 3.35 * cells; // minimum voltage, actual empty is 3.2, but below 3.4 this average cells usually drop very rapidly, so assume empty early rather than late.
let min = 3.35 * cells; // minimum voltage, actual empty is 3.2, but below 3.3 to 3.4 average cells usually drop very rapidly, so assume empty early rather than late.
let max = 4.05 * cells; // maximum voltage when fully charged. Should be 4.2 but Motion blinds seem to be charged less, which prolongs life. Also one expects to charge to full.
let perc = Math.round((voltage - min) * 100 / (max - min), 0); // this assumes linear, which isn't true but will do until around 3.4 volt per cell, and average is now 3.7 which is usually true for most cells.
return Math.min(100, Math.max(perc, 0));
Expand Down Expand Up @@ -342,7 +360,7 @@ class MotionDriver extends EventEmitter {
getDevices(type = undefined, filter = undefined) {
let devices = [];
for (let entry of this.devices.values())
if ((type == entry.id.deviceType ||
if ((type == entry.id.deviceType || type.includes(entry.id.deviceType) ||
((type == undefined || type == null) &&
entry.id.deviceType != this.DeviceType.Gateway && entry.id.deviceType != this.DeviceType.ChildGateway)) &&
(filter == undefined || filter(entry.id)))
Expand All @@ -356,7 +374,9 @@ class MotionDriver extends EventEmitter {
this.client.setBroadcast(true);
this.client.setMulticastTTL(128);
this.client.addMembership(MULTICAST_ADDRESS);
this.listening = true;
this.emit('listening');
this.send({ "msgType": "GetDeviceList", "msgID": this.getMessageID() });
}

onError(error) {
Expand Down Expand Up @@ -404,6 +424,7 @@ class MotionDriver extends EventEmitter {
}

onClose() {
this.listening = false;
this.emit('close');
}

Expand Down Expand Up @@ -436,9 +457,7 @@ class MotionDriver extends EventEmitter {
}

async connect() {
let id = this.getMessageID();
this.client.bind(UDP_PORT_RECEIVE, function() {
this.send({ "msgType": "GetDeviceList", "msgID": id });
this.emit('connect');
}.bind(this))
}
Expand Down Expand Up @@ -533,18 +552,23 @@ class MotionDriver extends EventEmitter {
async send(msg) {
let message = JSON.stringify(msg);
let gateway = this.getGateway(msg.mac, msg.deviceType);
let addr = this.multicast || gateway == undefined || gateway.gatewayAddress == null ? MULTICAST_ADDRESS : gateway.gatewayAddress;
let addr = this.ip;
if (addr == null || addr == undefined || addr == '')
addr = this.multicast || gateway == undefined || gateway.gatewayAddress == null || gateway.gatewayAddress == undefined
? MULTICAST_ADDRESS : gateway.gatewayAddress;
this.log('Sending ' + msg.msgType + ' to ' + addr + ' for ' + msg.mac + '-' + msg.deviceType);
if (this.verbose)
this.log(msg);
this.client.send(message, UDP_PORT_SEND, addr, function (error) {
if (error) {
this.error(error);
this.emit('error', error);
} else {
this.emit(message.msgType, message);
}
}.bind(this));
this.client.send(message, UDP_PORT_SEND, addr);

// , function (error) {
// if (error) {
// this.error(error);
// this.emit('senderror', error);
// } else {
// this.emit(message.msgType, message);
// }
// }.bind(this));
this.emit(msg.msgType, msg);
}
}
Expand Down
4 changes: 4 additions & 0 deletions settings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ <h2 data-i18n="settings.title"></h2>
<label for="motion_key" data-i18n="settings.keyField"></label>
<input id="motion_key" type="password" value="" maxlength="16" pattern="[\-0-9A-Za-z]{16}"/>
</div>
<div class="field row">
<label for="motion_ip" data-i18n="settings.ip"></label>
<input id="motion_ip" type="text"/>
</div>
</fieldset>
<fieldset>
<legend data-i18n="settings.options"></legend>
Expand Down
13 changes: 13 additions & 0 deletions settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

function onHomeyReady(Homey) {
var keyElement = document.getElementById('motion_key');
var ipElement = document.getElementById('motion_ip');
var debugElement = document.getElementById('debug');
var multicastElement = document.getElementById('multicast');
var clearElement = document.getElementById('clear');
Expand All @@ -12,6 +13,11 @@ function onHomeyReady(Homey) {
keyElement.value = motion_key;
});

Homey.get('motion_ip', function (err, motion_ip) {
if (err) return Homey.alert(err);
ipElement.value = motion_ip;
});

Homey.get('debug', function (err, debug) {
if (err) return Homey.alert(err);
debugElement.checked = debug;
Expand All @@ -26,6 +32,9 @@ function onHomeyReady(Homey) {
Homey.set('motion_key', keyElement.value, function (err) {
if (err) return Homey.alert(err);
});
Homey.set('motion_ip', ipElement.value, function (err) {
if (err) return Homey.alert(err);
});
Homey.set('debug', debugElement.checked, function (err) {
if (err) return Homey.alert(err);
});
Expand All @@ -39,6 +48,10 @@ function onHomeyReady(Homey) {
Homey.unset('motion_key', function (err) {
if (err) return Homey.alert(err);
});
ipElement.value = '';
Homey.unset('motion_ip', function (err) {
if (err) return Homey.alert(err);
});
debugElement.checked = false;
Homey.unset('debug', function (err) {
if (err) return Homey.alert(err);
Expand Down

0 comments on commit 97ccd40

Please sign in to comment.