@spectralblu/eufy-security-client
Version:
Client to comunicate with Eufy-Security devices, forked from bropat/eufy-client-security
933 lines • 132 kB
JavaScript
"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EufySecurity = void 0;
const tiny_typed_emitter_1 = require("tiny-typed-emitter");
const ts_log_1 = require("ts-log");
const fse = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const events_1 = __importDefault(require("events"));
const image_type_1 = __importDefault(require("image-type"));
const api_1 = require("./http/api");
const station_1 = require("./http/station");
const types_1 = require("./http/types");
const service_1 = require("./push/service");
const device_1 = require("./http/device");
const types_2 = require("./p2p/types");
const utils_1 = require("./utils");
const error_1 = require("./error");
const _1 = require(".");
const error_2 = require("./http/error");
const types_3 = require("./push/types");
const service_2 = require("./mqtt/service");
const const_1 = require("./http/const");
const utils_2 = require("./http/utils");
class EufySecurity extends tiny_typed_emitter_1.TypedEmitter {
config;
log;
api;
houses = {};
stations = {};
devices = {};
P2P_REFRESH_INTERVAL_MIN = 720;
cameraMaxLivestreamSeconds = 30;
cameraStationLivestreamTimeout = new Map();
pushService;
mqttService;
pushCloudRegistered = false;
pushCloudChecked = false;
persistentFile;
persistentData = {
country: "",
openudid: "",
serial_number: "",
push_credentials: undefined,
push_persistentIds: [],
login_hash: "",
version: "",
httpApi: undefined
};
connected = false;
retries = 0;
refreshEufySecurityCloudTimeout;
refreshEufySecurityP2PTimeout = {};
deviceSnoozeTimeout = {};
loadingEmitter = new events_1.default();
stationsLoaded = (0, utils_1.waitForEvent)(this.loadingEmitter, "stations loaded");
devicesLoaded = (0, utils_1.waitForEvent)(this.loadingEmitter, "devices loaded");
constructor(config, log = ts_log_1.dummyLogger) {
super();
this.config = config;
this.log = log;
}
static async initialize(config, log = ts_log_1.dummyLogger) {
const eufySecurity = new EufySecurity(config, log);
await eufySecurity._initializeInternals();
return eufySecurity;
}
async _initializeInternals() {
if (this.config.country === undefined) {
this.config.country = "US";
}
else {
this.config.country = this.config.country.toUpperCase();
}
if (this.config.language === undefined) {
this.config.language = "en";
}
if (this.config.eventDurationSeconds === undefined) {
this.config.eventDurationSeconds = 10;
}
if (this.config.p2pConnectionSetup === undefined) {
this.config.p2pConnectionSetup = types_2.P2PConnectionType.QUICKEST;
}
else if (!Object.values(types_2.P2PConnectionType).includes(this.config.p2pConnectionSetup)) {
this.config.p2pConnectionSetup = types_2.P2PConnectionType.QUICKEST;
}
if (this.config.pollingIntervalMinutes === undefined) {
this.config.pollingIntervalMinutes = 10;
}
if (this.config.acceptInvitations === undefined) {
this.config.acceptInvitations = false;
}
if (this.config.persistentDir === undefined) {
this.config.persistentDir = path.resolve(__dirname, "../../..");
}
else if (!fse.existsSync(this.config.persistentDir)) {
this.config.persistentDir = path.resolve(__dirname, "../../..");
}
this.persistentFile = path.join(this.config.persistentDir, "persistent.json");
try {
if (fse.statSync(this.persistentFile).isFile()) {
const fileContent = fse.readFileSync(this.persistentFile, "utf8");
this.persistentData = JSON.parse(fileContent);
}
}
catch (err) {
const error = (0, error_1.ensureError)(err);
this.log.debug("No stored data from last exit found", { error: (0, utils_1.getError)(error) });
}
try {
if (this.persistentData.version !== _1.libVersion) {
const currentVersion = Number.parseFloat((0, utils_1.removeLastChar)(_1.libVersion, "."));
const previousVersion = this.persistentData.version !== "" && this.persistentData.version !== undefined ? Number.parseFloat((0, utils_1.removeLastChar)(this.persistentData.version, ".")) : 0;
this.log.debug("Handling of driver update", { currentVersion: currentVersion, previousVersion: previousVersion });
if (previousVersion < currentVersion) {
this.persistentData = (0, utils_1.handleUpdate)(this.persistentData, this.log, previousVersion);
this.persistentData.version = _1.libVersion;
this.writePersistentData();
}
}
}
catch (err) {
const error = (0, error_1.ensureError)(err);
this.log.error("Handling update - Error", { error: (0, utils_1.getError)(error) });
}
if (this.config.trustedDeviceName === undefined || this.config.trustedDeviceName === "") {
if (this.persistentData.fallbackTrustedDeviceName !== undefined) {
this.config.trustedDeviceName = this.persistentData.fallbackTrustedDeviceName;
}
else {
const rnd = (0, utils_2.randomNumber)(0, const_1.PhoneModels.length);
this.persistentData.fallbackTrustedDeviceName = const_1.PhoneModels[rnd];
this.config.trustedDeviceName = this.persistentData.fallbackTrustedDeviceName;
}
}
this.api = await api_1.HTTPApi.initialize(this.config.country, this.config.username, this.config.password, this.log, this.persistentData.httpApi);
this.api.setLanguage(this.config.language);
this.api.setPhoneModel(this.config.trustedDeviceName);
this.api.on("houses", (houses) => this.handleHouses(houses));
this.api.on("hubs", (hubs) => this.handleHubs(hubs));
this.api.on("devices", (devices) => this.handleDevices(devices));
this.api.on("close", () => this.onAPIClose());
this.api.on("connect", () => this.onAPIConnect());
this.api.on("captcha request", (id, captcha) => this.onCaptchaRequest(id, captcha));
this.api.on("auth token invalidated", () => this.onAuthTokenInvalidated());
this.api.on("tfa request", () => this.onTfaRequest());
this.api.on("connection error", (error) => this.onAPIConnectionError(error));
if (this.persistentData.login_hash && this.persistentData.login_hash != "") {
this.log.debug("Load previous login_hash", { login_hash: this.persistentData.login_hash });
if ((0, utils_1.md5)(`${this.config.username}:${this.config.password}`) != this.persistentData.login_hash) {
this.log.info("Authentication properties changed, invalidate saved cloud token.");
this.persistentData.cloud_token = "";
this.persistentData.cloud_token_expiration = 0;
this.persistentData.httpApi = undefined;
}
}
else {
this.persistentData.cloud_token = "";
this.persistentData.cloud_token_expiration = 0;
}
if (this.persistentData.country !== undefined && this.persistentData.country !== "" && this.persistentData.country !== this.config.country) {
this.log.info("Country property changed, invalidate saved cloud token.");
this.persistentData.cloud_token = "";
this.persistentData.cloud_token_expiration = 0;
this.persistentData.httpApi = undefined;
}
if (this.persistentData.cloud_token && this.persistentData.cloud_token != "" && this.persistentData.cloud_token_expiration) {
this.log.debug("Load previous token", { token: this.persistentData.cloud_token, tokenExpiration: this.persistentData.cloud_token_expiration });
this.api.setToken(this.persistentData.cloud_token);
this.api.setTokenExpiration(new Date(this.persistentData.cloud_token_expiration));
}
if (this.persistentData.httpApi !== undefined && (this.persistentData.httpApi.clientPrivateKey === undefined || this.persistentData.httpApi.clientPrivateKey === "" || this.persistentData.httpApi.serverPublicKey === undefined || this.persistentData.httpApi.serverPublicKey === "")) {
this.log.debug("Incomplete persistent data for v2 encrypted cloud api communication. Invalidate authenticated session data.");
this.persistentData.cloud_token = "";
this.persistentData.cloud_token_expiration = 0;
this.persistentData.httpApi = undefined;
}
if (!this.persistentData.openudid || this.persistentData.openudid == "") {
this.persistentData.openudid = (0, utils_1.generateUDID)();
this.log.debug("Generated new openudid", { openudid: this.persistentData.openudid });
}
this.api.setOpenUDID(this.persistentData.openudid);
if (!this.persistentData.serial_number || this.persistentData.serial_number == "") {
this.persistentData.serial_number = (0, utils_1.generateSerialnumber)(12);
this.log.debug("Generated new serial_number", { serialnumber: this.persistentData.serial_number });
}
this.api.setSerialNumber(this.persistentData.serial_number);
this.pushService = new service_1.PushNotificationService(this.log);
this.pushService.on("connect", async (token) => {
this.pushCloudRegistered = await this.api.registerPushToken(token);
this.pushCloudChecked = await this.api.checkPushToken();
//TODO: Retry if failed with max retry to not lock account
if (this.pushCloudRegistered && this.pushCloudChecked) {
this.log.info("Push notification connection successfully established");
this.emit("push connect");
}
else {
this.log.info("Push notification connection closed");
this.emit("push close");
}
});
this.pushService.on("credential", (credentials) => {
this.savePushCredentials(credentials);
});
this.pushService.on("message", (message) => this.onPushMessage(message));
this.pushService.on("close", () => {
this.log.info("Push notification connection closed");
this.emit("push close");
});
await this.initMQTT();
}
async initMQTT() {
this.mqttService = await service_2.MQTTService.init(this.log);
this.mqttService.on("connect", () => {
this.log.info("MQTT connection successfully established");
this.emit("mqtt connect");
});
this.mqttService.on("close", () => {
this.log.info("MQTT connection closed");
this.emit("mqtt close");
});
this.mqttService.on("lock message", (message) => {
this.getDevice(message.data.data.deviceSn).then((device) => {
device.processMQTTNotification(message.data.data, this.config.eventDurationSeconds);
}).catch((err) => {
const error = (0, error_1.ensureError)(err);
if (!(error instanceof error_1.DeviceNotFoundError)) {
this.log.error("Lock MQTT Message Error", { error: (0, utils_1.getError)(error) });
}
}).finally(() => {
this.emit("mqtt lock message", message);
});
});
}
getPushService() {
return this.pushService;
}
addStation(station) {
const serial = station.getSerial();
if (serial && !Object.keys(this.stations).includes(serial)) {
this.stations[serial] = station;
this.getStorageInfo(serial);
this.emit("station added", station);
}
else {
this.log.debug(`Station with this serial ${station.getSerial()} exists already and couldn't be added again!`);
}
}
removeStation(station) {
const serial = station.getSerial();
if (serial && Object.keys(this.stations).includes(serial)) {
delete this.stations[serial];
station.removeAllListeners();
if (station.isConnected())
station.close();
this.emit("station removed", station);
}
else {
this.log.debug(`Station with this serial ${station.getSerial()} doesn't exists and couldn't be removed!`);
}
}
async updateStation(hub) {
if (this.stationsLoaded)
await this.stationsLoaded;
if (Object.keys(this.stations).includes(hub.station_sn)) {
this.stations[hub.station_sn].update(hub);
if (!this.stations[hub.station_sn].isConnected() && !this.stations[hub.station_sn].isEnergySavingDevice() && this.stations[hub.station_sn].isP2PConnectableDevice()) {
this.stations[hub.station_sn].setConnectionType(this.config.p2pConnectionSetup);
this.stations[hub.station_sn].connect();
}
this.getStorageInfo(hub.station_sn);
}
else {
this.log.debug(`Station with this serial ${hub.station_sn} doesn't exists and couldn't be updated!`);
}
}
async getStorageInfo(stationSerial) {
try {
const station = await this.getStation(stationSerial);
if (station.isStation() || (station.hasProperty(types_1.PropertyName.StationSdStatus) && station.getPropertyValue(types_1.PropertyName.StationSdStatus) !== undefined && station.getPropertyValue(types_1.PropertyName.StationSdStatus) !== types_2.TFCardStatus.REMOVE)) {
station.getStorageInfoEx();
}
}
catch (err) {
const error = (0, error_1.ensureError)(err);
this.log.error("getStorageInfo Error", { error: (0, utils_1.getError)(error), stationSN: stationSerial });
}
}
addDevice(device) {
const serial = device.getSerial();
if (serial && !Object.keys(this.devices).includes(serial)) {
this.devices[serial] = device;
this.emit("device added", device);
if (device.isLock())
this.mqttService.subscribeLock(device.getSerial());
}
else {
this.log.debug(`Device with this serial ${device.getSerial()} exists already and couldn't be added again!`);
}
}
removeDevice(device) {
const serial = device.getSerial();
if (serial && Object.keys(this.devices).includes(serial)) {
delete this.devices[serial];
device.removeAllListeners();
this.emit("device removed", device);
}
else {
this.log.debug(`Device with this serial ${device.getSerial()} doesn't exists and couldn't be removed!`);
}
}
async updateDevice(device) {
if (this.devicesLoaded)
await this.devicesLoaded;
if (Object.keys(this.devices).includes(device.device_sn))
this.devices[device.device_sn].update(device);
else
this.log.debug(`Device with this serial ${device.device_sn} doesn't exists and couldn't be updated!`);
}
async getDevices() {
if (this.devicesLoaded)
await this.devicesLoaded;
const arr = [];
Object.keys(this.devices).forEach((serialNumber) => {
arr.push(this.devices[serialNumber]);
});
return arr;
}
async getDevicesFromStation(stationSN) {
if (this.devicesLoaded)
await this.devicesLoaded;
const arr = [];
Object.keys(this.devices).forEach((serialNumber) => {
if (this.devices[serialNumber].getStationSerial() === stationSN)
arr.push(this.devices[serialNumber]);
});
return arr;
}
async getDevice(deviceSN) {
if (this.devicesLoaded)
await this.devicesLoaded;
if (Object.keys(this.devices).includes(deviceSN))
return this.devices[deviceSN];
throw new error_1.DeviceNotFoundError("Device doesn't exists", { context: { device: deviceSN } });
}
async getStationDevice(stationSN, channel) {
if (this.devicesLoaded)
await this.devicesLoaded;
for (const device of Object.values(this.devices)) {
if ((device.getStationSerial() === stationSN && device.getChannel() === channel) || (device.getStationSerial() === stationSN && device.getSerial() === stationSN)) {
return device;
}
}
throw new error_1.DeviceNotFoundError("No device with passed channel found on station", { context: { station: stationSN, channel: channel } });
}
async getStations() {
if (this.stationsLoaded)
await this.stationsLoaded;
const arr = [];
Object.keys(this.stations).forEach((serialNumber) => {
arr.push(this.stations[serialNumber]);
});
return arr;
}
async getStation(stationSN) {
if (this.stationsLoaded)
await this.stationsLoaded;
if (Object.keys(this.stations).includes(stationSN))
return this.stations[stationSN];
throw new error_1.StationNotFoundError("Station doesn't exists", { context: { station: stationSN } });
}
getApi() {
return this.api;
}
async connectToStation(stationSN, p2pConnectionType = types_2.P2PConnectionType.QUICKEST) {
const station = await this.getStation(stationSN);
if (station.isP2PConnectableDevice()) {
station.setConnectionType(p2pConnectionType);
await station.connect();
}
}
async isStationConnected(stationSN) {
const station = await this.getStation(stationSN);
return station.isConnected();
}
async isStationEnergySavingDevice(stationSN) {
const station = await this.getStation(stationSN);
return station.isEnergySavingDevice();
}
handleHouses(houses) {
this.log.debug("Got houses", { houses: houses });
//TODO: Finish implementation
this.houses = houses;
}
handleHubs(hubs) {
this.log.debug("Got hubs", { hubs: hubs });
const stationsSNs = Object.keys(this.stations);
const newStationsSNs = Object.keys(hubs);
const promises = [];
for (const hub of Object.values(hubs)) {
if (stationsSNs.includes(hub.station_sn)) {
this.updateStation(hub);
}
else {
if (this.stationsLoaded === undefined)
this.stationsLoaded = (0, utils_1.waitForEvent)(this.loadingEmitter, "stations loaded");
let ipAddress;
if (this.config.stationIPAddresses !== undefined) {
ipAddress = this.config.stationIPAddresses[hub.station_sn];
}
const station = station_1.Station.getInstance(this.api, hub, ipAddress);
promises.push(station.then((station) => {
try {
station.on("connect", (station) => this.onStationConnect(station));
station.on("connection error", (station, error) => this.onStationConnectionError(station, error));
station.on("close", (station) => this.onStationClose(station));
station.on("raw device property changed", (deviceSN, params) => this.updateDeviceProperties(deviceSN, params));
station.on("livestream start", (station, channel, metadata, videostream, audiostream) => this.onStartStationLivestream(station, channel, metadata, videostream, audiostream));
station.on("livestream stop", (station, channel) => this.onStopStationLivestream(station, channel));
station.on("livestream error", (station, channel, error) => this.onErrorStationLivestream(station, channel, error));
station.on("download start", (station, channel, metadata, videoStream, audioStream) => this.onStationStartDownload(station, channel, metadata, videoStream, audioStream));
station.on("download finish", (station, channel) => this.onStationFinishDownload(station, channel));
station.on("command result", (station, result) => this.onStationCommandResult(station, result));
station.on("guard mode", (station, guardMode) => this.onStationGuardMode(station, guardMode));
station.on("current mode", (station, currentMode) => this.onStationCurrentMode(station, currentMode));
station.on("rtsp livestream start", (station, channel) => this.onStartStationRTSPLivestream(station, channel));
station.on("rtsp livestream stop", (station, channel) => this.onStopStationRTSPLivestream(station, channel));
station.on("rtsp url", (station, channel, value) => this.onStationRtspUrl(station, channel, value));
station.on("property changed", (station, name, value, ready) => this.onStationPropertyChanged(station, name, value, ready));
station.on("raw property changed", (station, type, value) => this.onStationRawPropertyChanged(station, type, value));
station.on("alarm event", (station, alarmEvent) => this.onStationAlarmEvent(station, alarmEvent));
station.on("runtime state", (station, channel, batteryLevel, temperature) => this.onStationRuntimeState(station, channel, batteryLevel, temperature));
station.on("charging state", (station, channel, chargeType, batteryLevel) => this.onStationChargingState(station, channel, chargeType, batteryLevel));
station.on("wifi rssi", (station, channel, rssi) => this.onStationWifiRssi(station, channel, rssi));
station.on("floodlight manual switch", (station, channel, enabled) => this.onFloodlightManualSwitch(station, channel, enabled));
station.on("alarm delay event", (station, alarmDelayEvent, alarmDelay) => this.onStationAlarmDelayEvent(station, alarmDelayEvent, alarmDelay));
station.on("talkback started", (station, channel, talkbackStream) => this.onStationTalkbackStart(station, channel, talkbackStream));
station.on("talkback stopped", (station, channel) => this.onStationTalkbackStop(station, channel));
station.on("talkback error", (station, channel, error) => this.onStationTalkbackError(station, channel, error));
station.on("alarm armed event", (station) => this.onStationAlarmArmedEvent(station));
station.on("alarm arm delay event", (station, armDelay) => this.onStationArmDelayEvent(station, armDelay));
station.on("secondary command result", (station, result) => this.onStationSecondaryCommandResult(station, result));
station.on("device shake alarm", (deviceSN, event) => this.onStationDeviceShakeAlarm(deviceSN, event));
station.on("device 911 alarm", (deviceSN, event) => this.onStationDevice911Alarm(deviceSN, event));
station.on("device jammed", (deviceSN) => this.onStationDeviceJammed(deviceSN));
station.on("device low battery", (deviceSN) => this.onStationDeviceLowBattery(deviceSN));
station.on("device wrong try-protect alarm", (deviceSN) => this.onStationDeviceWrongTryProtectAlarm(deviceSN));
station.on("device pin verified", (deviceSN, successfull) => this.onStationDevicePinVerified(deviceSN, successfull));
station.on("sd info ex", (station, sdStatus, sdCapacity, sdCapacityAvailable) => this.onStationSdInfoEx(station, sdStatus, sdCapacity, sdCapacityAvailable));
station.on("image download", (station, file, image) => this.onStationImageDownload(station, file, image));
station.on("database query latest", (station, returnCode, data) => this.onStationDatabaseQueryLatest(station, returnCode, data));
station.on("database query local", (station, returnCode, data) => this.onStationDatabaseQueryLocal(station, returnCode, data));
station.on("database count by date", (station, returnCode, data) => this.onStationDatabaseCountByDate(station, returnCode, data));
station.on("database delete", (station, returnCode, failedIds) => this.onStationDatabaseDelete(station, returnCode, failedIds));
station.on("sensor status", (station, channel, status) => this.onStationSensorStatus(station, channel, status));
station.on("garage door status", (station, channel, doorId, status) => this.onStationGarageDoorStatus(station, channel, doorId, status));
station.on("storage info hb3", (station, channel, storageInfo) => this.onStorageInfoHb3(station, channel, storageInfo));
this.addStation(station);
station.initialize();
}
catch (err) {
const error = (0, error_1.ensureError)(err);
this.log.error("HandleHubs Error", { error: (0, utils_1.getError)(error), stationSN: station.getSerial() });
}
return station;
}));
}
}
Promise.all(promises).then(() => {
this.loadingEmitter.emit("stations loaded");
this.stationsLoaded = undefined;
});
if (promises.length === 0) {
this.loadingEmitter.emit("stations loaded");
this.stationsLoaded = undefined;
}
for (const stationSN of stationsSNs) {
if (!newStationsSNs.includes(stationSN)) {
this.getStation(stationSN).then((station) => {
this.removeStation(station);
}).catch((err) => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error removing station", { error: (0, utils_1.getError)(error), stationSN: stationSN });
});
}
}
}
onStationConnect(station) {
this.emit("station connect", station);
if (station_1.Station.isStation(station.getDeviceType()) || (device_1.Device.isCamera(station.getDeviceType()) && !device_1.Device.isWiredDoorbell(station.getDeviceType()) || device_1.Device.isSmartSafe(station.getDeviceType()))) {
station.getCameraInfo();
if (this.refreshEufySecurityP2PTimeout[station.getSerial()] !== undefined) {
clearTimeout(this.refreshEufySecurityP2PTimeout[station.getSerial()]);
delete this.refreshEufySecurityP2PTimeout[station.getSerial()];
}
this.refreshEufySecurityP2PTimeout[station.getSerial()] = setTimeout(() => {
station.getCameraInfo();
}, this.P2P_REFRESH_INTERVAL_MIN * 60 * 1000);
}
}
onStationConnectionError(station, error) {
this.emit("station connection error", station, error);
}
onStationClose(station) {
this.emit("station close", station);
for (const device_sn of this.cameraStationLivestreamTimeout.keys()) {
this.getDevice(device_sn).then((device) => {
if (device !== null && device.getStationSerial() === station.getSerial()) {
clearTimeout(this.cameraStationLivestreamTimeout.get(device_sn));
this.cameraStationLivestreamTimeout.delete(device_sn);
}
}).catch((err) => {
const error = (0, error_1.ensureError)(err);
this.log.error(`Station close Error`, { error: (0, utils_1.getError)(error), stationSN: station.getSerial() });
});
}
}
handleDevices(devices) {
this.log.debug("Got devices", { devices: devices });
const deviceSNs = Object.keys(this.devices);
const newDeviceSNs = Object.keys(devices);
const promises = [];
for (const device of Object.values(devices)) {
if (deviceSNs.includes(device.device_sn)) {
this.updateDevice(device);
}
else {
if (this.devicesLoaded === undefined)
this.devicesLoaded = (0, utils_1.waitForEvent)(this.loadingEmitter, "devices loaded");
let new_device;
if (device_1.Device.isIndoorCamera(device.device_type)) {
new_device = device_1.IndoorCamera.getInstance(this.api, device);
}
else if (device_1.Device.isSoloCameras(device.device_type)) {
new_device = device_1.SoloCamera.getInstance(this.api, device);
}
else if (device_1.Device.isBatteryDoorbell(device.device_type)) {
new_device = device_1.BatteryDoorbellCamera.getInstance(this.api, device);
}
else if (device_1.Device.isWiredDoorbell(device.device_type) || device_1.Device.isWiredDoorbellDual(device.device_type)) {
new_device = device_1.WiredDoorbellCamera.getInstance(this.api, device);
}
else if (device_1.Device.isFloodLight(device.device_type)) {
new_device = device_1.FloodlightCamera.getInstance(this.api, device);
}
else if (device_1.Device.isWallLightCam(device.device_type)) {
new_device = device_1.WallLightCam.getInstance(this.api, device);
}
else if (device_1.Device.isGarageCamera(device.device_type)) {
new_device = device_1.GarageCamera.getInstance(this.api, device);
}
else if (device_1.Device.isCamera(device.device_type)) {
new_device = device_1.Camera.getInstance(this.api, device);
}
else if (device_1.Device.isLock(device.device_type)) {
new_device = device_1.Lock.getInstance(this.api, device);
}
else if (device_1.Device.isMotionSensor(device.device_type)) {
new_device = device_1.MotionSensor.getInstance(this.api, device);
}
else if (device_1.Device.isEntrySensor(device.device_type)) {
new_device = device_1.EntrySensor.getInstance(this.api, device);
}
else if (device_1.Device.isKeyPad(device.device_type)) {
new_device = device_1.Keypad.getInstance(this.api, device);
}
else if (device_1.Device.isSmartSafe(device.device_type)) {
new_device = device_1.SmartSafe.getInstance(this.api, device);
}
else if (device_1.Device.isSmartTrack(device.device_type)) {
new_device = device_1.Tracker.getInstance(this.api, device);
}
else {
new_device = device_1.UnknownDevice.getInstance(this.api, device);
}
promises.push(new_device.then((device) => {
try {
device.on("property changed", (device, name, value, ready) => this.onDevicePropertyChanged(device, name, value, ready));
device.on("raw property changed", (device, type, value) => this.onDeviceRawPropertyChanged(device, type, value));
device.on("crying detected", (device, state) => this.onDeviceCryingDetected(device, state));
device.on("sound detected", (device, state) => this.onDeviceSoundDetected(device, state));
device.on("pet detected", (device, state) => this.onDevicePetDetected(device, state));
device.on("vehicle detected", (device, state) => this.onDeviceVehicleDetected(device, state));
device.on("motion detected", (device, state) => this.onDeviceMotionDetected(device, state));
device.on("person detected", (device, state, person) => this.onDevicePersonDetected(device, state, person));
device.on("rings", (device, state) => this.onDeviceRings(device, state));
device.on("locked", (device, state) => this.onDeviceLocked(device, state));
device.on("open", (device, state) => this.onDeviceOpen(device, state));
device.on("ready", (device) => this.onDeviceReady(device));
device.on("package delivered", (device, state) => this.onDevicePackageDelivered(device, state));
device.on("package stranded", (device, state) => this.onDevicePackageStranded(device, state));
device.on("package taken", (device, state) => this.onDevicePackageTaken(device, state));
device.on("someone loitering", (device, state) => this.onDeviceSomeoneLoitering(device, state));
device.on("radar motion detected", (device, state) => this.onDeviceRadarMotionDetected(device, state));
device.on("911 alarm", (device, state, detail) => this.onDevice911Alarm(device, state, detail));
device.on("shake alarm", (device, state, detail) => this.onDeviceShakeAlarm(device, state, detail));
device.on("wrong try-protect alarm", (device, state) => this.onDeviceWrongTryProtectAlarm(device, state));
device.on("long time not close", (device, state) => this.onDeviceLongTimeNotClose(device, state));
device.on("low battery", (device, state) => this.onDeviceLowBattery(device, state));
device.on("jammed", (device, state) => this.onDeviceJammed(device, state));
device.on("stranger person detected", (device, state) => this.onDeviceStrangerPersonDetected(device, state));
device.on("dog detected", (device, state) => this.onDeviceDogDetected(device, state));
device.on("dog lick detected", (device, state) => this.onDeviceDogLickDetected(device, state));
device.on("dog poop detected", (device, state) => this.onDeviceDogPoopDetected(device, state));
this.addDevice(device);
device.initialize();
}
catch (err) {
const error = (0, error_1.ensureError)(err);
this.log.error("HandleDevices Error", { error: (0, utils_1.getError)(error), deviceSN: device.getSerial() });
}
return device;
}));
}
}
Promise.all(promises).then((devices) => {
devices.forEach((device) => {
this.getStation(device.getStationSerial()).then((station) => {
if (!station.isConnected() && station.isP2PConnectableDevice()) {
station.setConnectionType(this.config.p2pConnectionSetup);
station.connect();
}
}).catch((err) => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error trying to connect to station afte device loaded", { error: (0, utils_1.getError)(error), deviceSN: device.getSerial() });
});
});
this.loadingEmitter.emit("devices loaded");
this.devicesLoaded = undefined;
});
if (promises.length === 0) {
this.loadingEmitter.emit("devices loaded");
this.devicesLoaded = undefined;
}
for (const deviceSN of deviceSNs) {
if (!newDeviceSNs.includes(deviceSN)) {
this.getDevice(deviceSN).then((device) => {
this.removeDevice(device);
}).catch((err) => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error removing device", { error: (0, utils_1.getError)(error), deviceSN: deviceSN });
});
}
}
}
async refreshCloudData() {
if (this.config.acceptInvitations) {
await this.processInvitations().catch(err => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error in processing invitations", { error: (0, utils_1.getError)(error) });
});
}
await this.api.refreshAllData().catch(err => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error during API data refreshing", { error: (0, utils_1.getError)(error) });
});
if (this.refreshEufySecurityCloudTimeout !== undefined)
clearTimeout(this.refreshEufySecurityCloudTimeout);
this.refreshEufySecurityCloudTimeout = setTimeout(() => { this.refreshCloudData(); }, this.config.pollingIntervalMinutes * 60 * 1000);
}
close() {
for (const device_sn of this.cameraStationLivestreamTimeout.keys()) {
this.stopStationLivestream(device_sn);
}
if (this.refreshEufySecurityCloudTimeout !== undefined)
clearTimeout(this.refreshEufySecurityCloudTimeout);
Object.keys(this.refreshEufySecurityP2PTimeout).forEach(station_sn => {
clearTimeout(this.refreshEufySecurityP2PTimeout[station_sn]);
delete this.refreshEufySecurityP2PTimeout[station_sn];
});
Object.keys(this.deviceSnoozeTimeout).forEach(device_sn => {
clearTimeout(this.deviceSnoozeTimeout[device_sn]);
delete this.deviceSnoozeTimeout[device_sn];
});
this.savePushPersistentIds();
this.pushService.close();
this.mqttService.close();
Object.values(this.stations).forEach(station => {
station.close();
});
Object.values(this.devices).forEach(device => {
device.destroy();
});
if (this.connected)
this.emit("close");
this.connected = false;
}
setCameraMaxLivestreamDuration(seconds) {
this.cameraMaxLivestreamSeconds = seconds;
}
getCameraMaxLivestreamDuration() {
return this.cameraMaxLivestreamSeconds;
}
async registerPushNotifications(credentials, persistentIds) {
if (credentials)
this.pushService.setCredentials(credentials);
if (persistentIds)
this.pushService.setPersistentIds(persistentIds);
this.pushService.open();
}
async connect(options) {
await this.api.login(options)
.then(async () => {
if (options?.verifyCode) {
let trusted = false;
const trusted_devices = await this.api.listTrustDevice();
trusted_devices.forEach(trusted_device => {
if (trusted_device.is_current_device === 1) {
trusted = true;
}
});
if (!trusted)
return await this.api.addTrustDevice(options?.verifyCode);
}
})
.catch((err) => {
const error = (0, error_1.ensureError)(err);
this.log.error("Connect Error", { error: (0, utils_1.getError)(error), options: options });
});
}
getPushPersistentIds() {
return this.pushService.getPersistentIds();
}
updateDeviceProperties(deviceSN, values) {
this.getDevice(deviceSN).then((device) => {
device.updateRawProperties(values);
}).catch((err) => {
const error = (0, error_1.ensureError)(err);
this.log.error("Update device properties error", { error: (0, utils_1.getError)(error), deviceSN: deviceSN, values: values });
});
}
async onAPIClose() {
if (this.refreshEufySecurityCloudTimeout !== undefined)
clearTimeout(this.refreshEufySecurityCloudTimeout);
this.connected = false;
this.emit("close");
if (this.retries < 3) {
this.retries++;
await this.connect();
}
else {
this.log.error(`Tried to re-authenticate to Eufy cloud, but failed in the process. Manual intervention is required!`);
}
}
async onAPIConnect() {
this.connected = true;
this.retries = 0;
this.saveCloudToken();
await this.refreshCloudData();
this.emit("connect");
this.registerPushNotifications(this.persistentData.push_credentials, this.persistentData.push_persistentIds);
const loginData = this.api.getPersistentData();
if (loginData) {
this.mqttService.connect(loginData.user_id, this.persistentData.openudid, this.api.getAPIBase(), loginData.email);
}
else {
this.log.warn("No login data recevied to initialize MQTT connection...");
}
}
onAPIConnectionError(error) {
this.emit("connection error", error);
}
async startStationLivestream(deviceSN) {
const device = await this.getDevice(deviceSN);
const station = await this.getStation(device.getStationSerial());
if (!device.hasCommand(types_1.CommandName.DeviceStartLivestream))
throw new error_1.NotSupportedError("This functionality is not implemented or supported by this device", { context: { device: deviceSN, commandName: types_1.CommandName.DeviceStartLivestream } });
const camera = device;
if (!station.isLiveStreaming(camera)) {
station.startLivestream(camera);
if (this.cameraMaxLivestreamSeconds > 0) {
this.cameraStationLivestreamTimeout.set(deviceSN, setTimeout(() => {
this.log.info(`Stopping the station stream for the device ${deviceSN}, because we have reached the configured maximum stream timeout (${this.cameraMaxLivestreamSeconds} seconds)`);
this.stopStationLivestream(deviceSN);
}, this.cameraMaxLivestreamSeconds * 1000));
}
}
else {
this.log.warn(`The station stream for the device ${deviceSN} cannot be started, because it is already streaming!`);
}
}
async stopStationLivestream(deviceSN) {
const device = await this.getDevice(deviceSN);
const station = await this.getStation(device.getStationSerial());
if (!device.hasCommand(types_1.CommandName.DeviceStopLivestream))
throw new error_1.NotSupportedError("This functionality is not implemented or supported by this device", { context: { device: deviceSN, commandName: types_1.CommandName.DeviceStopLivestream } });
if (station.isConnected() && station.isLiveStreaming(device)) {
station.stopLivestream(device);
}
else {
this.log.warn(`The station stream for the device ${deviceSN} cannot be stopped, because it isn't streaming!`);
}
const timeout = this.cameraStationLivestreamTimeout.get(deviceSN);
if (timeout) {
clearTimeout(timeout);
this.cameraStationLivestreamTimeout.delete(deviceSN);
}
}
writePersistentData() {
this.persistentData.login_hash = (0, utils_1.md5)(`${this.config.username}:${this.config.password}`);
this.persistentData.httpApi = this.api?.getPersistentData();
this.persistentData.country = this.api?.getCountry();
try {
fse.writeFileSync(this.persistentFile, JSON.stringify(this.persistentData));
}
catch (err) {
const error = (0, error_1.ensureError)(err);
this.log.error("WritePersistentData Error", { error: (0, utils_1.getError)(error) });
}
}
saveCloudToken() {
const token = this.api.getToken();
const token_expiration = this.api.getTokenExpiration();
if (!!token && !!token_expiration) {
this.log.debug("Save cloud token and token expiration", { token: token, tokenExpiration: token_expiration });
this.persistentData.cloud_token = token;
this.persistentData.cloud_token_expiration = token_expiration.getTime();
this.writePersistentData();
}
}
savePushCredentials(credentials) {
this.persistentData.push_credentials = credentials;
this.writePersistentData();
}
savePushPersistentIds() {
this.persistentData.push_persistentIds = this.getPushPersistentIds();
this.writePersistentData();
}
getVersion() {
return _1.libVersion;
}
isPushConnected() {
return this.pushService.isConnected();
}
isMQTTConnected() {
return this.mqttService.isConnected();
}
isConnected() {
return this.connected;
}
async processInvitations() {
let refreshCloud = false;
const invites = await this.api.getInvites().catch(err => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error getting invites from cloud", { error: (0, utils_1.getError)(error) });
return error;
});
if (Object.keys(invites).length > 0) {
const confirmInvites = [];
for (const invite of Object.values(invites)) {
const devices = [];
invite.devices.forEach(device => {
devices.push(device.device_sn);
});
if (devices.length > 0) {
confirmInvites.push({
invite_id: invite.invite_id,
station_sn: invite.station_sn,
device_sns: devices
});
}
}
if (confirmInvites.length > 0) {
const result = await this.api.confirmInvites(confirmInvites).catch(err => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error in confirmation of invitations", { error: (0, utils_1.getError)(error), confirmInvites: confirmInvites });
return error;
});
if (result) {
this.log.info(`Accepted received invitations`, confirmInvites);
refreshCloud = true;
}
}
}
const houseInvites = await this.api.getHouseInviteList().catch(err => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error getting house invites from cloud", { error: (0, utils_1.getError)(error) });
return error;
});
if (Object.keys(houseInvites).length > 0) {
for (const invite of Object.values(houseInvites)) {
const result = await this.api.confirmHouseInvite(invite.house_id, invite.id).catch(err => {
const error = (0, error_1.ensureError)(err);
this.log.error("Error in confirmation of house invitations", { error: (0, utils_1.getError)(error) });
return error;
});
if (result) {
this.log.info(`Accepted received house invitation from ${invite.action_user_email}`, { invite: invite });
refreshCloud = true;
}
}
}
if (refreshCloud)
this.refreshCloudData();
}
onPushMessage(message) {
this.emit("push message", message);
try {
this.log.debug("Received push message", { message: message });
try {
if ((message.type === types_3.ServerPushEvent.INVITE_DEVICE || message.type === types_3.ServerPushEvent.HOUSE_INVITE) && this.config.acceptInvitations) {
if (this.isConnected())
this.processInvitations();
}
}
catch (err) {
const error = (0, error_1.ensureError)(err);
this.log.error(`Error proc