homebridge-lgwebos-tv
Version:
Homebridge plugin to control LG webOS TV.
116 lines (95 loc) • 3.5 kB
JavaScript
import { promises as fsPromises } from 'fs';
import { createConnection } from "net";
import { DiacriticsMap } from './constants.js';
class Functions {
constructor() {
}
async saveData(path, data, stringify = true) {
try {
data = stringify ? JSON.stringify(data, null, 2) : data;
await fsPromises.writeFile(path, data);
return true;
} catch (error) {
throw new Error(`Save data error: ${error}`);
}
}
async readData(path, parseJson = false) {
try {
const data = await fsPromises.readFile(path, 'utf8');
if (parseJson) {
if (!data.trim()) {
// Empty file when expecting JSON
return null;
}
try {
return JSON.parse(data);
} catch (jsonError) {
throw new Error(`JSON parse error in file "${path}": ${jsonError.message}`);
}
}
// For non-JSON, just return file content (can be empty string)
return data;
} catch (error) {
if (error.code === 'ENOENT') {
// File does not exist
return null;
}
// Preserve original error details
const wrappedError = new Error(`Read data error for "${path}": ${error.message}`);
wrappedError.original = error;
throw wrappedError;
}
}
async sanitizeString(str) {
if (!str) return '';
// Replace diacritics using map
str = str.replace(/[^\u0000-\u007E]/g, ch => DiacriticsMap[ch] || ch);
// Replace separators between words with space
str = str.replace(/(\w)[.:;+\-\/]+(\w)/g, '$1 $2');
// Replace remaining standalone separators with space
str = str.replace(/[.:;+\-\/]/g, ' ');
// Remove remaining invalid characters (keep letters, digits, space, apostrophe)
str = str.replace(/[^A-Za-z0-9 ']/g, ' ');
// Collapse multiple spaces
str = str.replace(/\s+/g, ' ');
// Trim
return str.trim();
}
async scaleValue(value, inMin, inMax, outMin, outMax) {
const scaledValue = parseFloat((((Math.max(inMin, Math.min(inMax, value)) - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin).toFixed(0));
return scaledValue;
}
async ping(host, port, timeout = 3000) {
return new Promise((resolve) => {
const startTime = Date.now();
const socket = createConnection({ host, port });
let finished = false;
const finish = (result) => {
if (finished) return;
finished = true;
socket.destroy();
resolve(result);
};
socket.setTimeout(timeout);
socket.once("connect", () => {
finish({
online: true,
time: Date.now() - startTime
});
});
socket.once("timeout", () => {
finish({
online: false,
reason: "timeout"
});
});
socket.once("error", (error) => {
finish({
online: false,
reason: error.code || "error"
});
});
});
}
}
export default Functions