import * as net from 'net'; import { Appliance } from './appliance.js'; import { DaikinBRP069 } from './devices/brp069.js'; import { DaikinBRP084 } from './devices/brp084.js'; import { DaikinBRP072C } from './devices/brp072c.js'; import { DaikinAirBase } from './devices/airbase.js'; import { DaikinSkyFi } from './devices/skyfi.js'; import { getName } from './discovery.js'; import { DaikinException } from './exceptions.js'; export interface DaikinFactoryOptions { deviceId: string; password?: string; key?: string; uuid?: string; } export async function DaikinFactory( deviceId: string, options: DaikinFactoryOptions, ): Promise { const { password, key, uuid } = options; const { deviceIp, devicePort } = extractIpPort(deviceId); let obj: Appliance | null = null; if (password) { obj = new DaikinSkyFi(deviceIp, password); } else if (key) { obj = new DaikinBRP072C(deviceIp, { key, uuid }); } if (!obj) { try { obj = new DaikinBRP084(deviceIp); await obj.updateStatus(); if (!obj.values.get('mode')) { obj.values.set('mode', 'off'); obj.values.set('pow', '0'); } } catch { obj = null; } } if (!obj) { try { obj = new DaikinBRP069(deviceIp); if (devicePort && devicePort !== 80) { obj.baseUrl = `http://${deviceIp}:${devicePort}`; } await obj.updateStatus(DaikinBRP069.HTTP_RESOURCES.slice(0, 1)); if (obj.values.size === 0) { throw new DaikinException('Empty Values.'); } } catch { obj = null; } } if (!obj) { obj = new DaikinAirBase(deviceIp); if (devicePort && devicePort !== 80) { obj.baseUrl = `http://${deviceIp}:${devicePort}`; } } await obj.init(); if (!obj.values.get('mode')) { throw new DaikinException( `Error creating device, ${deviceId} is not supported.`, ); } return obj; } function extractIpPort(deviceId: string): { deviceIp: string; devicePort: number | null } { const portMatch = deviceId.match(/^(.+):(\d+)$/); if (portMatch) { return { deviceIp: portMatch[1], devicePort: parseInt(portMatch[2], 10) }; } try { const deviceName = getName(deviceId); if (deviceName && deviceName.port) { return { deviceIp: deviceName.ip, devicePort: deviceName.port }; } } catch { // Ignore discovery errors } return { deviceIp: deviceId, devicePort: null }; }