From 961c47a47305f52c259d793cf8cf8e34df66d870 Mon Sep 17 00:00:00 2001 From: Grzegorz Date: Sun, 25 Aug 2024 16:45:30 +0200 Subject: [PATCH] release v3.1.1 --- CHANGELOG.md | 7 +++ index.js | 34 ++++++++++--- package.json | 2 +- src/lgwebosdevice.js | 43 ++++------------- src/lgwebossocket.js | 110 +++++++++++++++++++++++++------------------ 5 files changed, 107 insertions(+), 89 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c5c840..648826c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### After update to v3.0.0 RESTFull and MQTT config settings need to be updated +## [3.1.1] - (25.08.2024) + +## Changes + +- fix [#220](https://github.com/grzegorz914/homebridge-lgwebos-tv/issues/220) +- cleanup + ## [3.1.0] - (23.08.2024) ## Changes diff --git a/index.js b/index.js index 38f5ae2..900e638 100644 --- a/index.js +++ b/index.js @@ -13,12 +13,6 @@ class LgWebOsPlatform { }; this.accessories = []; - //check if prefs directory exist - const prefDir = path.join(api.user.storagePath(), 'lgwebosTv'); - if (!fs.existsSync(prefDir)) { - fs.mkdirSync(prefDir); - }; - api.on('didFinishLaunching', () => { for (const device of config.devices) { const deviceName = device.name; @@ -43,8 +37,34 @@ class LgWebOsPlatform { }; const debug1 = enableDebugMode ? log.info(`Device: ${host} ${deviceName}, Config: ${JSON.stringify(config, null, 2)}`) : false; + //define directory and file paths + const prefDir = path.join(api.user.storagePath(), 'lgwebosTv'); + const postFix = host.split('.').join(''); + const keyFile = `${prefDir}/key_${postFix}`; + const devInfoFile = `${prefDir}/devInfo_${postFix}`; + const inputsFile = `${prefDir}/inputs_${postFix}`; + const channelsFile = `${prefDir}/channels_${postFix}`; + const inputsNamesFile = `${prefDir}/inputsNames_${postFix}`; + const inputsTargetVisibilityFile = `${prefDir}/inputsTargetVisibility_${postFix}`; + const files = [keyFile, devInfoFile, inputsFile, channelsFile, inputsNamesFile, inputsTargetVisibilityFile]; + + try { + //create directory if it doesn't exist + fs.mkdirSync(prefDir, { recursive: true }); + + //create files if they don't exist + files.forEach((file) => { + if (!fs.existsSync(file)) { + fs.writeFileSync(file, ''); + } + }); + } catch (error) { + this.emit('error', `prepare files error: ${error}`); + return; + } + //webos device - const lgWebOsDevice = new LgWebOsDevice(api, prefDir, device); + const lgWebOsDevice = new LgWebOsDevice(api, device, keyFile, devInfoFile, inputsFile, channelsFile, inputsNamesFile, inputsTargetVisibilityFile); lgWebOsDevice.on('publishAccessory', (accessory) => { api.publishExternalAccessories(CONSTANTS.PluginName, [accessory]); log.success(`Device: ${host} ${deviceName}, published as external accessory.`); diff --git a/package.json b/package.json index b91d09c..87106c7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "displayName": "LG webOS TV", "name": "homebridge-lgwebos-tv", - "version": "3.1.0", + "version": "3.1.1", "description": "Homebridge plugin to control LG webOS TV.", "license": "MIT", "author": "grzegorz914", diff --git a/src/lgwebosdevice.js b/src/lgwebosdevice.js index 8157dfa..bc19f7d 100644 --- a/src/lgwebosdevice.js +++ b/src/lgwebosdevice.js @@ -10,7 +10,7 @@ const CONSTANTS = require('./constants.json'); let Accessory, Characteristic, Service, Categories, Encode, AccessoryUUID; class LgWebOsDevice extends EventEmitter { - constructor(api, prefDir, device) { + constructor(api, device, keyFile, devInfoFile, inputsFile, channelsFile, inputsNamesFile, inputsTargetVisibilityFile) { super(); Accessory = api.platformAccessory; @@ -105,6 +105,8 @@ class LgWebOsDevice extends EventEmitter { this.soundMode = ''; this.soundOutput = ''; this.invertMediaState = false; + this.inputsNamesFile = inputsNamesFile; + this.inputsTargetVisibilityFile = inputsTargetVisibilityFile; //picture mode variable this.picturesModesConfigured = []; @@ -194,35 +196,6 @@ class LgWebOsDevice extends EventEmitter { } this.buttonsConfiguredCount = this.buttonsConfigured.length || 0; - //check files exists, if not then create it - const postFix = this.host.split('.').join(''); - const keyFile = `${prefDir}/key_${postFix}`; - const devInfoFile = `${prefDir}/devInfo_${postFix}`; - const inputsFile = `${prefDir}/inputs_${postFix}`; - const channelsFile = `${prefDir}/channels_${postFix}`; - this.inputsNamesFile = `${prefDir}/inputsNames_${postFix}`; - this.inputsTargetVisibilityFile = `${prefDir}/inputsTargetVisibility_${postFix}`; - - try { - const files = [ - keyFile, - devInfoFile, - inputsFile, - channelsFile, - this.inputsNamesFile, - this.inputsTargetVisibilityFile - ]; - - files.forEach((file) => { - if (!fs.existsSync(file)) { - fs.writeFileSync(file, ''); - } - }); - } catch (error) { - this.emit('error', `prepare files error: ${error}`); - return; - } - //Wake On Lan this.wol = new Wol({ mac: this.mac, @@ -727,7 +700,7 @@ class LgWebOsDevice extends EventEmitter { this.televisionService.setCharacteristic(Characteristic.DisplayOrder, Encode(1, displayOrder).toString('base64')); return true;; } catch (error) { - throw new Error(error); + throw new Error(`Display order error: ${error.message ?? error}`); }; } @@ -737,7 +710,7 @@ class LgWebOsDevice extends EventEmitter { const debug = !this.enableDebugMode ? false : this.emit('debug', `Saved data: ${JSON.stringify(data, null, 2)}`); return true;; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); }; } @@ -746,7 +719,7 @@ class LgWebOsDevice extends EventEmitter { const data = await fsPromises.readFile(path); return data; } catch (error) { - throw new Error(`Read saved data error: ${error}`); + throw new Error(`Read saved data error: ${error.message ?? error}`); }; } @@ -868,7 +841,7 @@ class LgWebOsDevice extends EventEmitter { }; return set; } catch (error) { - throw new Error(`${integration} set key: ${key}, value: ${value}, error: ${error}`); + throw new Error(`${integration} set key: ${key}, value: ${value}, error: ${error.message ?? error}`); }; } @@ -2028,7 +2001,7 @@ class LgWebOsDevice extends EventEmitter { return accessory; } catch (error) { - throw new Error(error) + throw new Error(error.message ?? error) }; }; }; diff --git a/src/lgwebossocket.js b/src/lgwebossocket.js index d0e84de..cfc0c6c 100644 --- a/src/lgwebossocket.js +++ b/src/lgwebossocket.js @@ -766,7 +766,7 @@ class LgWebOsSocket extends EventEmitter { const pairingKey = key.length > 10 ? key.toString() : '0'; return pairingKey; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); } } @@ -775,7 +775,7 @@ class LgWebOsSocket extends EventEmitter { await fsPromises.writeFile(path, pairingKey); return true; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); } } @@ -795,7 +795,7 @@ class LgWebOsSocket extends EventEmitter { return true; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); }; }; @@ -807,7 +807,7 @@ class LgWebOsSocket extends EventEmitter { return true; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); } }; @@ -819,7 +819,7 @@ class LgWebOsSocket extends EventEmitter { return true; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); } }; @@ -835,7 +835,7 @@ class LgWebOsSocket extends EventEmitter { this.emit('prepareAccessory'); return true; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); } } @@ -883,7 +883,7 @@ class LgWebOsSocket extends EventEmitter { } return true; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); }; } @@ -916,51 +916,61 @@ class LgWebOsSocket extends EventEmitter { return cid; } } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); } } - async send(type, uri, payload = {}, cid, title = 'Unknown Title', message = 'Unknown Message') { + async send(type, uri, payload, cid, title, message) { try { - if (!this.socketConnected && type !== 'button') { - this.emit('warn', 'Socket not connected.'); - return; - } - - if (type === 'button' && !this.specializedSocketConnected) { - this.emit('warn', 'Specialized socket not connected.'); - return; - } + payload = payload ?? {}; + cid = cid ?? await this.getCid(); + title = title ?? 'Unknown Title'; + message = message ?? 'Unknown Message'; + let data = {}; + let messageContent = {}; - let data, messageContent; switch (type) { case 'button': + if (!this.specjalizedSocketConnected) { + this.emit('warn', 'Specialized socket not connected.'); + return; + }; + const keyValuePairs = Object.entries(payload).map(([key, value]) => `${key}:${value}`); keyValuePairs.unshift(`type:${type}`); - messageContent = keyValuePairs.join('\n') + '\n\n'; - this.specializedSocket.send(messageContent); - break; + const array = keyValuePairs.join('\n') + '\n\n'; + + this.specializedSocket.send(array); + return true; case 'alert': - this.alertCid = cid; - const alertPayload = { - title: title, - message: message, - modal: true, - buttons: [{ label: 'Ok', focus: true, buttonType: 'ok', onClick: uri, params: payload }], - onclose: { uri: uri, params: payload }, - onfail: { uri: uri, params: payload }, - type: 'confirm', - isSysReq: true + if (!this.socketConnected) { + this.emit('warn', 'Socket not connected.'); + return; }; + + this.alertCid = cid; + const buttons = [{ label: 'Ok', focus: true, buttonType: 'ok', onClick: uri, params: payload }]; + const onClose = { uri: uri, params: payload }; + const onFail = { uri: uri, params: payload }; + const alertPayload = { title: title, message: message, modal: true, buttons: buttons, onclose: onClose, onfail: onFail, type: 'confirm', isSysReq: true }; data = { id: cid, type: 'request', uri: CONSTANTS.ApiUrls.CreateAlert, payload: alertPayload }; - break; + + messageContent = JSON.stringify(data); + this.socket.send(messageContent); + const debug = this.debugLog ? this.emit('debug', `Alert send: ${messageContent}`) : false; + return true; case 'toast': + if (!this.socketConnected) { + this.emit('warn', 'Socket not connected.'); + return; + }; + this.toastCid = cid; const toastPayload = { message: message, iconData: null, iconExtension: null, onClick: payload }; data = { @@ -969,23 +979,31 @@ class LgWebOsSocket extends EventEmitter { uri: CONSTANTS.ApiUrls.CreateToast, payload: toastPayload }; - break; + + messageContent = JSON.stringify(data); + this.socket.send(messageContent); + const debug1 = this.debugLog ? this.emit('debug', `Toast send: ${messageContent}`) : false; + return true; default: - data = { id: cid, type: type, uri: uri, payload: payload }; - break; - } + if (!this.socketConnected) { + this.emit('warn', 'Socket not connected.'); + return; + }; - if (type !== 'button') { - messageContent = JSON.stringify(data); - this.socket.send(messageContent); - } + data = { + id: cid, + type: type, + uri: uri, + payload: payload + }; - if (this.debugLog) { - this.emit('debug', `${type.charAt(0).toUpperCase() + type.slice(1)} send: ${messageContent}`); - } - return true; + messageContent = JSON.stringify(data); + this.socket.send(messageContent); + const debug2 = this.debugLog ? this.emit('debug', `Socket send: ${messageContent}`) : false; + return true; + }; } catch (error) { - throw new Error(error); + throw new Error(error.message ?? error); } } };