diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 2913b3f..1ecd69d 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -512,7 +512,6 @@ it("Should connect to API with demo credentials", async () => { - `admin/` - Admin UI configuration (JSON Config) and translations - `lib/` - Shared libraries and utilities - `lib/stateAttr.js` - State attribute definitions for meter readings - - `lib/tools.js` - Utility functions including translation tools - `lib/adapter-config.d.ts` - TypeScript definitions for adapter config - `test/` - Test files (package, integration, unit, mocha setup) - `.github/workflows/` - CI/CD configuration (test-and-release.yml) diff --git a/admin/i18n/de/translations.json b/admin/i18n/de/translations.json index bd144ed..50e0e44 100644 --- a/admin/i18n/de/translations.json +++ b/admin/i18n/de/translations.json @@ -1,5 +1,6 @@ { "Password": "Kennwort", + "Seconds between data polling": "Sekunden zwischen den Datenabfragen", "Username": "Benutzername", "Your Discovergy Username": "Ihr Discovergy-Benutzername", "discovergy adapter settings": "Adaptereinstellungen für discovergy", diff --git a/admin/i18n/es/translations.json b/admin/i18n/es/translations.json index 23a4ec2..112afa2 100644 --- a/admin/i18n/es/translations.json +++ b/admin/i18n/es/translations.json @@ -1,5 +1,6 @@ { "Password": "La opción 2 no es", + "Seconds between data polling": "Segundos entre sondeos de datos", "Username": "La opción 1 es genial", "Your Discovergy Username": "Su nombre de usuario de Discovergy", "discovergy adapter settings": "Ajustes del adaptador para discovergy", diff --git a/admin/i18n/fr/translations.json b/admin/i18n/fr/translations.json index cd7b38f..e22de9b 100644 --- a/admin/i18n/fr/translations.json +++ b/admin/i18n/fr/translations.json @@ -1,5 +1,6 @@ { "Password": "L'option 2 n'est pas", + "Seconds between data polling": "Secondes entre les interrogations de données", "Username": "L'option 1 est cool", "Your Discovergy Username": "Votre nom d'utilisateur Discovergy", "discovergy adapter settings": "Paramètres d'adaptateur pour discovergy", diff --git a/admin/i18n/it/translations.json b/admin/i18n/it/translations.json index 52a94f5..b4a91dd 100644 --- a/admin/i18n/it/translations.json +++ b/admin/i18n/it/translations.json @@ -1,5 +1,6 @@ { "Password": "L'opzione 2 no", + "Seconds between data polling": "Secondi tra i polling dei dati", "Username": "L'opzione 1 è cool", "Your Discovergy Username": "Il tuo nome utente Discovergy", "discovergy adapter settings": "Impostazioni dell'adattatore per discovergy", diff --git a/admin/i18n/nl/translations.json b/admin/i18n/nl/translations.json index cf98913..b6e4f16 100644 --- a/admin/i18n/nl/translations.json +++ b/admin/i18n/nl/translations.json @@ -1,5 +1,6 @@ { "Password": "Optie 2 is dat niet", + "Seconds between data polling": "Seconden tussen het ophalen van gegevens", "Username": "Optie 1 is cool", "Your Discovergy Username": "Uw Discoverygy-gebruikersnaam", "discovergy adapter settings": "Adapterinstellingen voor discovergy", diff --git a/admin/i18n/pl/translations.json b/admin/i18n/pl/translations.json index d6eff9b..fbcd389 100644 --- a/admin/i18n/pl/translations.json +++ b/admin/i18n/pl/translations.json @@ -1,5 +1,6 @@ { "Password": "Opcja 2 nie jest", + "Seconds between data polling": "Sekundy między odpytywaniem danych", "Username": "Opcja 1 jest fajna", "Your Discovergy Username": "Twoja nazwa użytkownika Discovery", "discovergy adapter settings": "Ustawienia adaptera dla discovergy", diff --git a/admin/i18n/pt/translations.json b/admin/i18n/pt/translations.json index b8ae410..0e00f5f 100644 --- a/admin/i18n/pt/translations.json +++ b/admin/i18n/pt/translations.json @@ -1,5 +1,6 @@ { "Password": "A opção 2 não é", + "Seconds between data polling": "Segundos entre as consultas de dados", "Username": "Opção 1 é legal", "Your Discovergy Username": "Seu nome de usuário do Discovergy", "discovergy adapter settings": "Configurações do adaptador para discovergy", diff --git a/admin/i18n/ru/translations.json b/admin/i18n/ru/translations.json index e89a1af..edde9fe 100644 --- a/admin/i18n/ru/translations.json +++ b/admin/i18n/ru/translations.json @@ -1,5 +1,6 @@ { "Password": "Вариант 2 не", + "Seconds between data polling": "Секунды между опросами данных", "Username": "Вариант 1 - это круто", "Your Discovergy Username": "Ваше имя пользователя Discovery", "discovergy adapter settings": "Настройки адаптера для discovergy", diff --git a/admin/i18n/uk/translations.json b/admin/i18n/uk/translations.json index a1adef5..5cfb5aa 100644 --- a/admin/i18n/uk/translations.json +++ b/admin/i18n/uk/translations.json @@ -1,5 +1,6 @@ { "Password": "Пароль", + "Seconds between data polling": "Секунди між опитуваннями даних", "Username": "Ім'я користувача", "Your Discovergy Username": "Ваше ім'я користувача Discovergy", "discovergy adapter settings": "Налаштування адаптера discovergy", diff --git a/admin/i18n/zh-cn/translations.json b/admin/i18n/zh-cn/translations.json index 3820bf7..4c40276 100644 --- a/admin/i18n/zh-cn/translations.json +++ b/admin/i18n/zh-cn/translations.json @@ -1,5 +1,6 @@ { "Password": "密码", + "Seconds between data polling": "数据轮询间隔秒数", "Username": "用户名", "Your Discovergy Username": "您的发现用户名", "discovergy adapter settings": "用于发现的适配器设置", diff --git a/lib/tools.js b/lib/tools.js deleted file mode 100644 index 9b8ae7b..0000000 --- a/lib/tools.js +++ /dev/null @@ -1,121 +0,0 @@ -// Native fetch is available in Node.js 18+ - -/** - * Tests whether the given variable is a real object and not an Array - * - * @param {any} it The variable to test - * @returns {it is Record} - */ -function isObject(it) { - // This is necessary because: - // typeof null === 'object' - // typeof [] === 'object' - // [] instanceof Object === true - return Object.prototype.toString.call(it) === '[object Object]'; -} - -/** - * Tests whether the given variable is really an Array - * - * @param {any} it The variable to test - * @returns {it is any[]} - */ -function isArray(it) { - if (typeof Array.isArray === 'function') { - return Array.isArray(it); - } - return Object.prototype.toString.call(it) === '[object Array]'; -} - -/** - * Translates text to the target language. Automatically chooses the right translation API. - * - * @param {string} text The text to translate - * @param {string} targetLang The target languate - * @param {string} [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers - * @returns {Promise} - */ -async function translateText(text, targetLang, yandexApiKey) { - if (targetLang === 'en') { - return text; - } - if (yandexApiKey) { - return await translateYandex(text, targetLang, yandexApiKey); - } - return await translateGoogle(text, targetLang); -} - -/** - * Translates text with Yandex API - * - * @param {string} text The text to translate - * @param {string} targetLang The target languate - * @param {string} [apiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers - * @returns {Promise} - */ -async function translateYandex(text, targetLang, apiKey) { - if (targetLang === 'zh-cn') { - targetLang = 'zh'; - } - try { - const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`; - const controller = new AbortController(); - const timeoutId = setTimeout(() => controller.abort(), 15000); - - const response = await fetch(url, { - signal: controller.signal, - }); - clearTimeout(timeoutId); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data = await response.json(); - if (data && data['text']) { - return data['text'][0]; - } - throw new Error('Invalid response for translate request'); - } catch (e) { - throw new Error(`Could not translate to "${targetLang}": ${e}`); - } -} - -/** - * Translates text with Google API - * - * @param {string} text The text to translate - * @param {string} targetLang The target languate - * @returns {Promise} - */ -async function translateGoogle(text, targetLang) { - try { - const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}&ie=UTF-8&oe=UTF-8`; - const controller = new AbortController(); - const timeoutId = setTimeout(() => controller.abort(), 15000); - - const response = await fetch(url, { - signal: controller.signal, - }); - clearTimeout(timeoutId); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data = await response.json(); - if (isArray(data)) { - // we got a valid response - return data[0][0][0]; - } - throw new Error('Invalid response for translate request'); - } catch (e) { - throw new Error(`Could not translate to "${targetLang}": ${e}`); - } -} - -module.exports = { - isArray, - isObject, - translateText, -}; diff --git a/main.js b/main.js index f793cab..da80925 100644 --- a/main.js +++ b/main.js @@ -201,9 +201,9 @@ class Discovergy extends utils.Adapter { // New data polling at intervall time if (timer) { - clearTimeout(timer); + this.clearTimeout(timer); } - timer = setTimeout(() => { + timer = this.setTimeout(() => { this.dataPolling(); }, settings.intervall); } @@ -455,7 +455,7 @@ class Discovergy extends utils.Adapter { this.setState('info.connection', false, true); this.log.info('cleaned everything up...'); if (timer) { - clearTimeout(timer); + this.clearTimeout(timer); timer = null; } callback();