* (Apollon77) Implement Service triggers to use any value to trigger or stringified JSON to call with fields

This commit is contained in:
Ingo Fischer
2022-03-21 17:11:48 +01:00
parent b887a75ede
commit 3b83e686f7
21 changed files with 433 additions and 251 deletions
+5 -4
View File
@@ -92,7 +92,7 @@ function HASS(options, log) {
}
}
function callService(socket, service, domain, serviceData, callback) {
function callService(socket, service, domain, serviceData, target, callback) {
if (socket && typeof socket.send === 'function') {
const id = currentId++;
requests[id] = {type: 'call_service', cb: callback, ts: Date.now()};
@@ -101,7 +101,8 @@ function HASS(options, log) {
type: 'call_service',
domain: domain || '',
service: service,
service_data: serviceData
service_data: serviceData,
target
}));
} else {
callback && callback('not connected');
@@ -228,11 +229,11 @@ function HASS(options, log) {
}
};
this.callService = function (service, domain, serviceData, callback) {
this.callService = function (service, domain, serviceData, target, callback) {
if (!connected) {
typeof callback === 'function' && callback('not connected');
} else {
callService(this.socket, service, domain, serviceData, callback);
callService(this.socket, service, domain, serviceData, target, callback);
}
};
+92
View File
@@ -0,0 +1,92 @@
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>}
*/
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 (Array.isArray != null)
return Array.isArray(it);
return Object.prototype.toString.call(it) === "[object Array]";
}
/**
* Choose the right tranalation API
* @param {string} text The text to translate
* @param {string} targetLang The target languate
* @param {string} yandex api key
* @returns {Promise<string>}
*/
async function translateText(text, targetLang, yandex) {
if (targetLang === "en") {
return text;
}
if (yandex) {
return await translateYandex(text, targetLang, yandex);
} else {
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} yandex api key
* @returns {Promise<string>}
*/
async function translateYandex(text, targetLang, yandex) {
if (targetLang === "zh-cn") {
targetLang = "zh";
}
try {
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${yandex}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
const response = await axios({url, timeout: 15000});
if (response.data && response.data['text']) {
return response.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<string>}
*/
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 response = await axios({url, timeout: 15000});
if (isArray(response.data)) {
// we got a valid response
return response.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
};