Skip to content

Commit

Permalink
(simatec) Code fix
Browse files Browse the repository at this point in the history
  • Loading branch information
simatec committed Dec 3, 2024
1 parent c873deb commit 366f760
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ When the adapter crashes or an other Code error happens, this error message that
<!-- ### __WORK IN PROGRESS__ -->
### __WORK IN PROGRESS__
* (simatec) eslint-config fix
* (simatec) Code fix

### 0.6.4 (2024-11-24)
* (simatec) json5 added
Expand Down
16 changes: 15 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ export default [
// you may disable some 'jsdoc' warnings - but using jsdoc is highly recommended
// as this improves maintainability. jsdoc warnings will not block buiuld process.
rules: {
// 'jsdoc/require-jsdoc': 'off',
'jsdoc/require-jsdoc': 'off',
'no-async-promise-executor': 'off',
'prettier/prettier': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'curly': 'off',
'jsdoc/require-returns-description': 'off',
'no-else-return': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
//'no-prototype-builtins': 'off',
//'no-case-declarations': 'off',
//'no-useless-escape': 'off',
//'jsdoc/require-param': 'off',
//'@typescript-eslint/no-require-imports': 'off',
//'jsdoc/no-types': 'off',
//'jsdoc/tag-lines': 'off',
},
},

Expand Down
41 changes: 23 additions & 18 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const axios = require('axios');

/**
* Tests whether the given variable is a real object and not an Array
* @param {any} it The variable to test
* @returns {it is Record<string, any>}
*
* @param it The variable to test
* @returns
*/
function isObject(it) {
// This is necessary because:
Expand All @@ -15,8 +16,9 @@ function isObject(it) {

/**
* Tests whether the given variable is really an Array
* @param {any} it The variable to test
* @returns {it is any[]}
*
* @param it The variable to test
* @returns
*/
function isArray(it) {
if (typeof Array.isArray === 'function') return Array.isArray(it);
Expand All @@ -25,10 +27,11 @@ function isArray(it) {

/**
* 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<string>}
*
* @param text The text to translate
* @param targetLang The target languate
* @param [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
* @returns
*/
async function translateText(text, targetLang, yandexApiKey) {
if (targetLang === 'en') {
Expand All @@ -45,10 +48,11 @@ async function translateText(text, targetLang, yandexApiKey) {

/**
* 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<string>}
*
* @param text The text to translate
* @param targetLang The target languate
* @param apiKey The yandex API key. You can create one for free at https://translate.yandex.com/developers
* @returns
*/
async function translateYandex(text, targetLang, apiKey) {
if (targetLang === 'zh-cn') {
Expand All @@ -57,7 +61,7 @@ async function translateYandex(text, targetLang, apiKey) {
try {
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
// @ts-ignore
const response = await axios({url, timeout: 15000});
const response = await axios({ url, timeout: 15000 });
if (response.data && response.data.text && isArray(response.data.text)) {
return response.data.text[0];
}
Expand All @@ -69,15 +73,16 @@ async function translateYandex(text, targetLang, apiKey) {

/**
* Translates text with Google API
* @param {string} text The text to translate
* @param {string} targetLang The target languate
* @returns {Promise<string>}
*
* @param text The text to translate
* @param targetLang The target languate
* @returns
*/
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`;
// @ts-ignore
const response = await axios({url, timeout: 15000});
const response = await axios({ url, timeout: 15000 });
if (isArray(response.data)) {
// we got a valid response
return response.data[0][0][0];
Expand All @@ -101,7 +106,7 @@ function _(word, systemLang) {
if (translations[word]) {
return translations[word];
} else {
console.warn('Please translate in translations.json: ' + word);
console.warn(`Please translate in translations.json: ${word}`);
return word;
}
}
Expand Down
23 changes: 12 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const adapterName = require('./package.json').name.split('.').pop();

/**
* Starts the adapter instance
*
* @param {Partial<ioBroker.AdapterOptions>} [options]
*/

Expand Down Expand Up @@ -50,16 +51,16 @@ function getSystemData() {

if (obj) {
systemLang = obj.common.language;
adapter.log.debug('System language: ' + systemLang);
adapter.log.debug(`System language: ${systemLang}`);

if (adapter.config.systemGeoData) {
longitude = obj.common.longitude;
latitude = obj.common.latitude;
adapter.log.debug('System longitude: ' + longitude + ' System latitude: ' + latitude);
adapter.log.debug(`System longitude: ${longitude} System latitude: ${latitude}`);
} else {
longitude = adapter.config.longitude;
latitude = adapter.config.latitude;
adapter.log.debug('longitude: ' + longitude + ' latitude: ' + latitude);
adapter.log.debug(`longitude: ${longitude} latitude: ${latitude}`);
}

// @ts-ignore
Expand Down Expand Up @@ -114,12 +115,12 @@ async function requestAPI() {

// created json
let json = ({
"uv": Math.round(openUVRequest.data.result.uv * 100) / 100,
"uv_time": new Date(openUVRequest.data.result.uv_time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
"uv_max": Math.round(openUVRequest.data.result.uv_max * 100) / 100,
"uv_max_time": new Date(openUVRequest.data.result.uv_max_time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
"ozone": openUVRequest.data.result.ozone,
"ozone_time": new Date(openUVRequest.data.result.ozone_time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
uv: Math.round(openUVRequest.data.result.uv * 100) / 100,
uv_time: new Date(openUVRequest.data.result.uv_time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
uv_max: Math.round(openUVRequest.data.result.uv_max * 100) / 100,
uv_max_time: new Date(openUVRequest.data.result.uv_max_time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
ozone: openUVRequest.data.result.ozone,
ozone_time: new Date(openUVRequest.data.result.ozone_time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
});

await adapter.setStateAsync('uv_json', JSON.stringify(json), true);
Expand Down Expand Up @@ -188,7 +189,7 @@ async function requestAPI() {
adapter.log.warn('API is not reachable');
}
} catch (err) {
adapter.log.warn('Request error: ' + err);
adapter.log.warn(`Request error: ${err}`);
}
// @ts-ignore
resolve();
Expand All @@ -211,7 +212,7 @@ async function main() {
}
}

// @ts-ignore parent is a valid property on module
// @ts-expect-error parent is a valid property on module
if (module.parent) {
// Export startAdapter in compact mode
module.exports = startAdapter;
Expand Down

0 comments on commit 366f760

Please sign in to comment.