UNPKG

homebridge-nordpool-baltics

Version:

Plugin exposes virtual accessories (switch, light, presence, temperature) and enables HomeKit automation by Nordpool electricity price in Lithuania, Latvia, Estonia, Finland, Sweden, Norway, Denmark, Germany, Luxembourg, Austria.

186 lines 7.61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fnc_currentHour = exports.fnc_tomorrowKey = exports.fnc_todayKey = exports.defaultAreaTimezone = exports.defaultPricesCache = exports.defaultService = exports.pricing = exports.devices = exports.PLATFORM_SERIAL_NUMBER = exports.PLATFORM_MODEL = exports.PLATFORM_VERSION = exports.PLATFORM_MANUFACTURER = exports.PLUGIN_NAME = exports.PLATFORM_NAME = void 0; const Path = __importStar(require("path")); const fs = __importStar(require("fs")); const luxon_1 = require("luxon"); const file_system_cache_1 = require("file-system-cache"); /* eslint @typescript-eslint/no-var-requires: "off" */ const pkg = require('../package.json'); exports.PLATFORM_NAME = 'Nordpool'; exports.PLUGIN_NAME = pkg.name; exports.PLATFORM_MANUFACTURER = pkg.author.name; exports.PLATFORM_VERSION = pkg.version; exports.PLATFORM_MODEL = 'Electricity price sensors'; exports.PLATFORM_SERIAL_NUMBER = 'UN783GU921Y0'; // main device(s) exports.devices = [ { UniqueId: 'JKGhJH654*87pDE', displayName: 'Nordpool', }, ]; exports.pricing = { today: [], currently: 0.0001, currentHour: 0, cheapestHour: [], cheapest4Hours: [], cheapest5Hours: [], cheapest5HoursConsec: [], cheapest5HoursConsec2days: [], cheapest6Hours: [], cheapest7Hours: [], cheapest8Hours: [], cheapest9Hours: [], cheapest10Hours: [], cheapest11Hours: [], cheapest12Hours: [], priciestHour: [], median: 0, median2days: 0, }; exports.defaultService = { currently: null, cheapestHour: null, cheapest4Hours: null, cheapest5Hours: null, cheapest5HoursConsec: null, cheapest6Hours: null, cheapest7Hours: null, cheapest8Hours: null, cheapest9Hours: null, cheapest10Hours: null, cheapest11Hours: null, cheapest12Hours: null, priciestHour: null, hourlyTickerSwitch: null, }; function defaultPricesCache(api, log) { const ns = 'homebridge-nordpool-baltics'; const nsHash = 'b162cf22c8adb8fa829628b261839cad18dc3994'; const storagePath = api.user.storagePath(); const cacheDirectory = Path.join(storagePath, '.cache'); const fallbackDirectory = storagePath; // Fallback to root storage path let finalCacheDirectory = cacheDirectory; try { // Ensure .cache directory exists if (!fs.existsSync(cacheDirectory)) { fs.mkdirSync(cacheDirectory, { recursive: true }); log.debug(`OK: Cache directory created at ${cacheDirectory}`); } // Check if directory is writable fs.accessSync(cacheDirectory, fs.constants.W_OK); } catch (error) { // If .cache directory creation or access fails, fall back to root storage path log.warn(`Failed to access or create cache directory at ${cacheDirectory}: ${error instanceof Error ? error.message : 'Unknown error'}`); log.warn(`Falling back to root storage path: ${fallbackDirectory}`); finalCacheDirectory = fallbackDirectory; } // Auto-cleanup of old cached files on init const files = fs.readdirSync(finalCacheDirectory); const now = Date.now(); files.filter(file => file.startsWith(`${nsHash}-`)).forEach(file => { const filePath = Path.join(finalCacheDirectory, file); try { const stats = fs.statSync(filePath); const fileAge = now - stats.mtimeMs; // If the file is older than 2 days, clean up if (fileAge >= 172800 * 1000 * 2) { fs.unlinkSync(filePath); log.debug(`OK: Deleted old cache file: ${filePath}`); return; } // Check if file is valid JSON const content = fs.readFileSync(filePath, 'utf8'); JSON.parse(content); } catch (error) { if (error instanceof SyntaxError) { log.warn(`Corrupted cache file detected and removed: ${filePath}`); try { fs.unlinkSync(filePath); } catch (unlinkError) { log.error(`Failed to delete corrupted cache file: ${filePath}`); } } else { log.warn(`Failed to access file stats or delete file: ${filePath}. Error: ${error instanceof Error ? error.message : 'Unknown error'}`); } } }); return new file_system_cache_1.Cache({ basePath: finalCacheDirectory, ns: ns, ttl: 172800 }); } exports.defaultPricesCache = defaultPricesCache; // Dynamically determine the timezone based on the configured area function defaultAreaTimezone(config) { var _a; const area = (_a = config.area) === null || _a === void 0 ? void 0 : _a.toUpperCase(); // Define timezone mappings for supported areas const timezoneMapping = { LT: 'Europe/Vilnius', // Lithuania LV: 'Europe/Riga', // Latvia EE: 'Europe/Tallinn', // Estonia FI: 'Europe/Helsinki', // Finland SE1: 'Europe/Stockholm', // Sweden SE2: 'Europe/Stockholm', // Sweden SE3: 'Europe/Stockholm', // Sweden SE4: 'Europe/Stockholm', // Sweden DK1: 'Europe/Copenhagen', // Denmark DK1 DK2: 'Europe/Copenhagen', // Denmark DK2 NO1: 'Europe/Oslo', // Norway NO1 NO2: 'Europe/Oslo', // Norway NO2 NO3: 'Europe/Oslo', // Norway NO3 NO4: 'Europe/Oslo', // Norway NO4 NO5: 'Europe/Oslo', // Norway NO5 DE: 'Europe/Berlin', // Germany LU: 'Europe/Luxembourg', // Luxembourg AT: 'Europe/Vienna', // Austria ES: 'Europe/Madrid', // Spain PT: 'Europe/Lisbon', // Portugal }; // Return the corresponding timezone or fallback to a default (e.g., Europe/Vilnius) return timezoneMapping[area] || 'Europe/Vilnius'; } exports.defaultAreaTimezone = defaultAreaTimezone; function fnc_todayKey(config) { const timezone = defaultAreaTimezone(config); return luxon_1.DateTime.local().setZone(timezone).toFormat('yyyy-MM-dd'); } exports.fnc_todayKey = fnc_todayKey; function fnc_tomorrowKey(config) { const timezone = defaultAreaTimezone(config); return luxon_1.DateTime.local().plus({ day: 1 }).setZone(timezone).toFormat('yyyy-MM-dd'); } exports.fnc_tomorrowKey = fnc_tomorrowKey; function fnc_currentHour(config) { const timezone = defaultAreaTimezone(config); return luxon_1.DateTime.local().setZone(timezone).hour; } exports.fnc_currentHour = fnc_currentHour; //# sourceMappingURL=settings.js.map