iobroker.amtronwallbox 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/tools.js ADDED
@@ -0,0 +1,99 @@
1
+ const axios = require("axios");
2
+
3
+ /**
4
+ * Tests whether the given variable is a real object and not an Array
5
+ * @param {any} it The variable to test
6
+ * @returns {it is Record<string, any>}
7
+ */
8
+ function isObject(it) {
9
+ // This is necessary because:
10
+ // typeof null === 'object'
11
+ // typeof [] === 'object'
12
+ // [] instanceof Object === true
13
+ return Object.prototype.toString.call(it) === "[object Object]";
14
+ }
15
+
16
+ /**
17
+ * Tests whether the given variable is really an Array
18
+ * @param {any} it The variable to test
19
+ * @returns {it is any[]}
20
+ */
21
+ function isArray(it) {
22
+ if (typeof Array.isArray === "function") return Array.isArray(it);
23
+ return Object.prototype.toString.call(it) === "[object Array]";
24
+ }
25
+
26
+ /**
27
+ * Translates text to the target language. Automatically chooses the right translation API.
28
+ * @param {string} text The text to translate
29
+ * @param {string} targetLang The target languate
30
+ * @param {string} [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
31
+ * @returns {Promise<string>}
32
+ */
33
+ async function translateText(text, targetLang, yandexApiKey) {
34
+ if (targetLang === "en") {
35
+ return text;
36
+ } else if (!text) {
37
+ return "";
38
+ }
39
+ if (yandexApiKey) {
40
+ return translateYandex(text, targetLang, yandexApiKey);
41
+ } else {
42
+ return translateGoogle(text, targetLang);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Translates text with Yandex API
48
+ * @param {string} text The text to translate
49
+ * @param {string} targetLang The target languate
50
+ * @param {string} apiKey The yandex API key. You can create one for free at https://translate.yandex.com/developers
51
+ * @returns {Promise<string>}
52
+ */
53
+ async function translateYandex(text, targetLang, apiKey) {
54
+ if (targetLang === "zh-cn") {
55
+ targetLang = "zh";
56
+ }
57
+ try {
58
+ const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
59
+ const response = await axios({url, timeout: 15000});
60
+ if (response.data && response.data.text && isArray(response.data.text)) {
61
+ return response.data.text[0];
62
+ }
63
+ throw new Error("Invalid response for translate request");
64
+ } catch (e) {
65
+ throw new Error(`Could not translate to "${targetLang}": ${e}`);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Translates text with Google API
71
+ * @param {string} text The text to translate
72
+ * @param {string} targetLang The target languate
73
+ * @returns {Promise<string>}
74
+ */
75
+ async function translateGoogle(text, targetLang) {
76
+ try {
77
+ 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`;
78
+ const response = await axios({url, timeout: 15000});
79
+ if (isArray(response.data)) {
80
+ // we got a valid response
81
+ return response.data[0][0][0];
82
+ }
83
+ throw new Error("Invalid response for translate request");
84
+ } catch (e) {
85
+ if (e.response && e.response.status === 429) {
86
+ throw new Error(
87
+ `Could not translate to "${targetLang}": Rate-limited by Google Translate`
88
+ );
89
+ } else {
90
+ throw new Error(`Could not translate to "${targetLang}": ${e}`);
91
+ }
92
+ }
93
+ }
94
+
95
+ module.exports = {
96
+ isArray,
97
+ isObject,
98
+ translateText
99
+ };