Skip to content

Commit

Permalink
browser_mod: add support for hideHeader
Browse files Browse the repository at this point in the history
also bring register stuff in line with what HomeAssistant does.

still need to find a way to configure browser_mod.
  • Loading branch information
Garfonso committed Dec 8, 2023
1 parent 61efe42 commit 0366f19
Showing 1 changed file with 129 additions and 13 deletions.
142 changes: 129 additions & 13 deletions lib/modules/browser_mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ class BrowserModModule {
this.adapter = options.adapter;
this.objects = options.objects;
this.clients = {};
this.browserModStorage = {
browsers: {},
version: '2.0',
settings: {
//possible settings. Send them all.
hideSidebar: true, //set this to true for now -> might make change in frontend unnecessary. And maybe we can use it later.
hideHeader: false,
defaultPanel: null,
sidebarPanelOrder: null,
sidebarHiddenPanels: null,
sidebarTitle: null,
faviconTemplate: null,
titleTemplate: null,
hideInteractIcon: true, //is the icon to configure browser_mod -> not working currently, so let's hide it.
autoRegister: true,
lockRegister: null
},
user_settings: {} //hm.. what is this? -> you can select settings on a per user basis. Proably stored here. Let's try that sometime.
};

this.knownViews = [];
this.knownViewsStates = {};
Expand Down Expand Up @@ -208,15 +227,58 @@ class BrowserModModule {
read: true,
write: false,
role: 'indicator.reachable',
default: true
def: true
}, native: {instance: browserId}
});
}

if (!this.objects[ioBrokerDeviceId + '.hideHeader']) {
await this.adapter.setObjectNotExistsAsync(ioBrokerDeviceId + '.hideHeader', {
type: 'state',
common: {
name: 'Hide Header',
type: 'boolean',
read: true,
write: false,
role: 'switch',
default: this.browserModStorage.settings.hideHeader
},
native: {
instance: browserId
}
});
} else {
//read hideHeader setting from object:
const hideHeader = await this.adapter.getStateAsync(ioBrokerDeviceId + '.hideHeader');
if (browserId) {
this.initialiseBrowserSettings(browserId);
this.browserModStorage.browsers[browserId].settings.hideHeader = hideHeader.val;
} else {
this.browserModStorage.settings.hideHeader = hideHeader.val;
}
}
}

initialiseBrowserSettings(browserId) {
if (!this.browserModStorage.browsers[browserId]) {
this.browserModStorage.browsers[browserId] = {
last_seen: 0,
registered: true,
locked: false,
camera: false,
settings: this.browserModStorage.settings, //send default settings...?
meta: 'default'
};
}
}

//store infos about browser instance
async _handleUpdate(ioBrokerDeviceId, message) {
if (message.browserID && this.browserModStorage.browsers[message.browserID]) {
this.browserModStorage.browsers[message.browserID].last_seen = Date.now() / 1000;
}
if (message.data) {

if (message.data.browser) {
//check if all objects in ioBroker are created.
if (message.data.browser.battery_level) {
Expand Down Expand Up @@ -279,27 +341,59 @@ class BrowserModModule {
instance: message.browserID,
ws
};
const settings = {
browsers: {
}
};
settings.browsers[message.browserID] = { registered: false };
ws.send(JSON.stringify({id: message.id, type: 'result', success: true, result: { connected: true }}));

ws.send(JSON.stringify({id: message.id, type: 'event', event: {
ws.send(JSON.stringify([{id: message.id, type: 'result', success: true, result: null }, {id: message.id, type: 'event', event: {
event_type: 'ready',
origin: 'LOCAL',
result: settings,
result: this.browserModStorage,
time_fired: new Date().toISOString()
}}));
}}]));

if (this.objects[`${this.adapter.namespace}.${ioBrokerDeviceId}.online`]) {
await this.adapter.setStateAsync(ioBrokerDeviceId + '.online', true, true);
} else {
console.log('No objects for instance, yet..', `${ioBrokerDeviceId}.online`, this.objects[ioBrokerDeviceId], this.objects);
}
} else if (method === 'register') {
this.initialiseBrowserSettings(message.browserID);

// if data.browserID exists, browserID is changed -> copy stuff and delete old id.
if (message.data.browserID) {
for (const id of Object.keys(this.objects)) {
if (id.startsWith(ioBrokerDeviceId)) {
delete this.objects[id];
}
}
try {
await this.adapter.deleteObjectAsync(ioBrokerDeviceId, {recursive: true});
await this._checkObjects(ioBrokerDeviceId, message.data.browserID);
} catch (e) {
this.adapter.log.warn('Could not delete old instance objects in ' + ioBrokerDeviceId + ', please do so yourself. Error was: ' + e);
}
delete this.browserModStorage.browsers[message.browserID];
delete message.data.browserID;
this.browserModStorage.browsers[message.data.browserID] = message.data;
} else {
try {
await this._checkObjects(ioBrokerDeviceId, message.browserID);
} catch (e) {
this.adapter.log.warn('Could not delete old instance objects in ' + ioBrokerDeviceId + ', please do so yourself. Error was: ' + e);
}
if (this.objects[`${this.adapter.namespace}.${ioBrokerDeviceId}.online`]) {
await this.adapter.setStateAsync(ioBrokerDeviceId + '.online', true, true);
} else {
console.log('No objects for instance, yet..', `${ioBrokerDeviceId}.online`, this.objects[ioBrokerDeviceId], this.objects);
}
}
} else if (method === 'log') {
this.adapter.log.debug('Message from browser_mod: ' + message.message);
} else if (method === 'settings') {
//{"type":"browser_mod/settings","key":"autoRegister","value":true,"id":200} <- something like this...
if (message.key && this.browserModStorage.settings[message.key] !== undefined) {
this.browserModStorage.settings[message.key] = message.value;
this.adapter.log.debug('Updated browser_mod settings: ' + message.key + ' to ' + message.value);
}
//think about making this permanent somehow? -> but only if we find a way to allow browser_mod_settings panel.
} else {
this.adapter.log.warn('Unknown browser_mod method: ' + JSON.stringify(message));
}
Expand All @@ -317,7 +411,7 @@ class BrowserModModule {
allDevices = true;
}

const event = {
let event = {
event_type: 'browser_mod/command',
command,
origin: 'LOCAL',
Expand Down Expand Up @@ -396,6 +490,16 @@ class BrowserModModule {
break;
case 'refresh':
break;
case 'hideHeader':
if (allDevices) {
this.browserModStorage.settings.hideHeader = state.val;
} else {
this.browserModStorage.browsers[browserId].hideHeader = state.val;
}
event = {
result: this.browserModStorage
};
break;
default:
return;
}
Expand Down Expand Up @@ -457,8 +561,20 @@ class BrowserModModule {
await this._checkObjects(instancesPath.substring(0, instancesPath.length - 1));

for (const id of Object.keys(this.objects)) {
if (id.startsWith(this.adapter.namespace + '.' + instancesPath) && id.endsWith('.online')) {
await this.adapter.setState(id, false, true);
if (id.startsWith(this.adapter.namespace + '.' + instancesPath)) {
if (id.endsWith('.online')) {
await this.adapter.setState(id, false, true);
} else if (id.endsWith('hideHeader')) {
const hideHeader = await this.adapter.getStateAsync(id);
if (id === this.adapter.namespace + '.' + instancesPath + 'hideHeader') {
this.browserModStorage.settings.hideHeader = hideHeader.val;
} else {
//read hideHeader states from ioBroker objects.
const browserId = id.split('.')[3];
this.initialiseBrowserSettings(browserId);
this.browserModStorage.browsers[id.split('.')[3]].settings.hideHeader = hideHeader.val;
}
}
}
}
}
Expand Down

0 comments on commit 0366f19

Please sign in to comment.