Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Cache and restore bridge IP addresses into global settings #30

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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: 3 additions & 0 deletions Sources/com.elgato.philips-hue.sdPlugin/pi/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function connectElgatoStreamDeckSocket(inPort, inUUID, inRegisterEvent, inInfo,
else if (event === 'sendToPropertyInspector') {
// Save global cache
cache = jsonPayload;

// Save Cached Bridges
pi.saveCachedBridges(cache);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this location in the code... is there a better one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the process either. Saving all bridges right before loading them seems counterintuitive. ;) But let's speak about the process first, because as I mentioned it may not be a good idea ...


// Load bridges and lights
pi.loadBridges();
Expand Down
14 changes: 14 additions & 0 deletions Sources/com.elgato.philips-hue.sdPlugin/pi/js/pi.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ function PI(inContext, inLanguage, inStreamDeckVersion, inPluginVersion) {
document.getElementById('groups').label = instance.localization['GroupsTitle'];
}
};

// Save IP addresses for bridges into global settings
this.saveCachedBridges = cache => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the use of that function. I mean, I get your intention. But this function replaces the IP in the global settings every time by the ip from the cache. I don't get why doing this is better. An this would inflict every dhcp connected bridge too, or not?

let changed = false;
Object.keys(cache).forEach(id => {
if (globalSettings.bridges[id].ip != cache[id].ip) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weak typecheck. Also no check if globalSettings.bridges[id] is available on that object, might throw undefined errors.

globalSettings.bridges[id].ip = cache[id].ip;
changed = true;
}
});
if (changed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing. ;)

saveGlobalSettings(inContext);
}
}

// Show all paired bridges
this.loadBridges = () => {
Expand Down
11 changes: 11 additions & 0 deletions Sources/com.elgato.philips-hue.sdPlugin/plugin/js/philips/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ function Cache() {

// Init discovery variable to indicate that it ran already
discovery = {};

// init discovery from previously paired bridges
if (globalSettings.bridges !== undefined) {
Object.keys(globalSettings.bridges).forEach(id => {
if (globalSettings.bridges[id].hasOwnProperty('ip')) {
log('restoring cached bridge: ' + id + ' - ' + globalSettings.bridges[id].ip);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use template strings.

discovery[id] = { 'ip': globalSettings.bridges[id].ip };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is js, not json. Don't use quotes for object properties. ;) And mybe use indention like the rest of the code.

}
});
}

// Run discovery
Bridge.discover((inSuccess, inBridges) => {
Expand All @@ -45,6 +55,7 @@ function Cache() {
// For all discovered bridges
inBridges.forEach(inBridge => {
// Add new bridge to discovery object
log('discovered bridge: ' + inBridge.getID() + ' - ' + inBridge.getIP());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use template strings.

discovery[inBridge.getID()] = {
ip: inBridge.getIP()
};
Expand Down