eufy-security-client
Version:
Client to comunicate with Eufy-Security devices
884 lines (883 loc) • 149 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 fse = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const events_1 = __importDefault(require("events"));
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");
const logging_1 = require("./logging");
const typescript_logging_1 = require("typescript-logging");
const utils_3 = require("./p2p/utils");
class EufySecurity extends tiny_typed_emitter_1.TypedEmitter {
config;
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 = logging_1.dummyLogger) {
super();
this.config = config;
logging_1.InternalLogger.logger = log;
}
static async initialize(config, log = logging_1.dummyLogger) {
const eufySecurity = new EufySecurity(config, log);
await eufySecurity._initializeInternals();
return eufySecurity;
}
async _initializeInternals() {
if (this.config.logging) {
if (this.config.logging.level !== undefined && typeof this.config.logging.level === "number" && Object.values(typescript_logging_1.LogLevel).includes(this.config.logging.level))
(0, logging_1.setLoggingLevel)("all", this.config.logging.level);
if (this.config.logging.categories !== undefined && Array.isArray(this.config.logging.categories)) {
for (const category of this.config.logging.categories) {
if (typeof category === "object" &&
"category" in category &&
"level" in category &&
typeof category.level === "number" &&
Object.values(typescript_logging_1.LogLevel).includes(category.level) &&
typeof category.category === "string" &&
["main", "http", "p2p", "push", "mqtt"].includes(category.category.toLowerCase())) {
(0, logging_1.setLoggingLevel)(category.category.toLocaleLowerCase(), category.level);
}
}
}
}
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.enableEmbeddedPKCS1Support === undefined) {
this.config.enableEmbeddedPKCS1Support = false;
}
if (this.config.deviceConfig === undefined) {
this.config.deviceConfig = {
simultaneousDetections: true
};
}
if (this.config.persistentDir === undefined) {
this.config.persistentDir = path.resolve(__dirname, "../../..");
}
else if (!fse.existsSync(this.config.persistentDir)) {
this.config.persistentDir = path.resolve(__dirname, "../../..");
}
if (this.config.persistentData) {
this.persistentData = JSON.parse(this.config.persistentData);
}
else {
this.persistentFile = path.join(this.config.persistentDir, "persistent.json");
}
try {
if (!this.config.persistentData && 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);
logging_1.rootMainLogger.debug("No stored data from last exit found", { error: (0, utils_1.getError)(error) });
}
logging_1.rootMainLogger.debug("Loaded persistent data", { persistentData: this.persistentData });
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;
logging_1.rootMainLogger.debug("Handling of driver update", { currentVersion: currentVersion, previousVersion: previousVersion });
if (previousVersion < currentVersion) {
this.persistentData = (0, utils_1.handleUpdate)(this.persistentData, previousVersion);
this.persistentData.version = _1.libVersion;
}
}
}
catch (err) {
const error = (0, error_1.ensureError)(err);
logging_1.rootMainLogger.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;
}
}
if (this.persistentData.login_hash && this.persistentData.login_hash != "") {
logging_1.rootMainLogger.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) {
logging_1.rootMainLogger.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;
this.persistentData.httpApi = undefined;
}
if (this.persistentData.country !== undefined && this.persistentData.country !== "" && this.persistentData.country !== this.config.country) {
logging_1.rootMainLogger.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.httpApi !== undefined && (this.persistentData.httpApi.clientPrivateKey === undefined || this.persistentData.httpApi.clientPrivateKey === "" || this.persistentData.httpApi.serverPublicKey === undefined || this.persistentData.httpApi.serverPublicKey === "")) {
logging_1.rootMainLogger.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;
}
this.api = await api_1.HTTPApi.initialize(this.config.country, this.config.username, this.config.password, 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.cloud_token && this.persistentData.cloud_token != "" && this.persistentData.cloud_token_expiration) {
logging_1.rootMainLogger.debug("Load previous token", { token: this.persistentData.cloud_token, tokenExpiration: this.persistentData.cloud_token_expiration, persistentHttpApi: this.persistentData.httpApi });
this.api.setToken(this.persistentData.cloud_token);
this.api.setTokenExpiration(new Date(this.persistentData.cloud_token_expiration));
}
if (!this.persistentData.openudid || this.persistentData.openudid == "") {
this.persistentData.openudid = (0, utils_1.generateUDID)();
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.debug("Generated new serial_number", { serialnumber: this.persistentData.serial_number });
}
this.api.setSerialNumber(this.persistentData.serial_number);
this.pushService = await service_1.PushNotificationService.initialize();
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) {
logging_1.rootMainLogger.info("Push notification connection successfully established");
this.emit("push connect");
}
else {
logging_1.rootMainLogger.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", () => {
logging_1.rootMainLogger.info("Push notification connection closed");
this.emit("push close");
});
await this.initMQTT();
}
async initMQTT() {
this.mqttService = await service_2.MQTTService.init();
this.mqttService.on("connect", () => {
logging_1.rootMainLogger.info("MQTT connection successfully established");
this.emit("mqtt connect");
});
this.mqttService.on("close", () => {
logging_1.rootMainLogger.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)) {
logging_1.rootMainLogger.error("Lock MQTT Message Error", { error: (0, utils_1.getError)(error) });
}
}).finally(() => {
this.emit("mqtt lock message", message);
});
});
}
setLoggingLevel(category, level) {
if (typeof level === "number" &&
Object.values(typescript_logging_1.LogLevel).includes(level) &&
typeof category === "string" &&
["all", "main", "http", "p2p", "push", "mqtt"].includes(category.toLowerCase())) {
(0, logging_1.setLoggingLevel)(category, level);
}
}
getLoggingLevel(category) {
if (typeof category === "string" &&
["all", "main", "http", "p2p", "push", "mqtt"].includes(category.toLowerCase())) {
return (0, logging_1.getLoggingLevel)(category);
}
return -1;
}
setInternalLogger(logger) {
logging_1.InternalLogger.logger = logger;
}
getInternalLogger() {
return logging_1.InternalLogger.logger;
}
getPushService() {
return this.pushService;
}
addStation(station) {
const serial = station.getSerial();
if (serial && !Object.keys(this.stations).includes(serial)) {
this.stations[serial] = station;
this.emit("station added", station);
}
else {
logging_1.rootMainLogger.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 {
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.debug(`Updating station cloud data - initiate station connection to get local data over p2p`, { stationSN: hub.station_sn });
this.stations[hub.station_sn].connect();
}
}
else {
logging_1.rootMainLogger.debug(`Station with this serial ${hub.station_sn} doesn't exists and couldn't be updated!`);
}
}
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 {
logging_1.rootMainLogger.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 {
logging_1.rootMainLogger.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
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.debug(`Explicit request for p2p connection to the station`, { stationSN: station.getSerial() });
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) {
logging_1.rootMainLogger.debug("Got houses", { houses: houses });
//TODO: Finish implementation
this.houses = houses;
}
handleHubs(hubs) {
logging_1.rootMainLogger.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, 0, this.config.enableEmbeddedPKCS1Support);
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);
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.error("Error removing station", { error: (0, utils_1.getError)(error), stationSN: stationSN });
});
}
}
}
refreshP2PData(station) {
if (station.isStation() || (device_1.Device.isCamera(station.getDeviceType()) && !device_1.Device.isWiredDoorbell(station.getDeviceType()) || device_1.Device.isSmartSafe(station.getDeviceType()))) {
station.getCameraInfo();
}
if (device_1.Device.isLock(station.getDeviceType())) {
station.getLockParameters();
station.getLockStatus();
}
if (station.isStation() || (station.hasProperty(types_1.PropertyName.StationSdStatus) && station.getPropertyValue(types_1.PropertyName.StationSdStatus) !== types_2.TFCardStatus.REMOVE)) {
station.getStorageInfoEx();
}
}
onStationConnect(station) {
this.emit("station connect", station);
this.refreshP2PData(station);
if (this.refreshEufySecurityP2PTimeout[station.getSerial()] !== undefined) {
clearTimeout(this.refreshEufySecurityP2PTimeout[station.getSerial()]);
delete this.refreshEufySecurityP2PTimeout[station.getSerial()];
}
this.refreshEufySecurityP2PTimeout[station.getSerial()] = setTimeout(() => {
this.refreshP2PData(station);
}, 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);
logging_1.rootMainLogger.error(`Station close Error`, { error: (0, utils_1.getError)(error), stationSN: station.getSerial() });
});
}
}
handleDevices(devices) {
logging_1.rootMainLogger.debug("Got devices", { devices: devices });
const deviceSNs = Object.keys(this.devices);
const newDeviceSNs = Object.keys(devices);
const promises = [];
const deviceConfig = this.config.deviceConfig;
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, deviceConfig);
}
else if (device_1.Device.isSoloCameras(device.device_type)) {
new_device = device_1.SoloCamera.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isLockWifiVideo(device.device_type)) {
new_device = device_1.DoorbellLock.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isBatteryDoorbell(device.device_type)) {
new_device = device_1.BatteryDoorbellCamera.getInstance(this.api, device, deviceConfig);
}
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, deviceConfig);
}
else if (device_1.Device.isFloodLight(device.device_type)) {
new_device = device_1.FloodlightCamera.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isWallLightCam(device.device_type)) {
new_device = device_1.WallLightCam.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isGarageCamera(device.device_type)) {
new_device = device_1.GarageCamera.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isSmartDrop(device.device_type)) {
new_device = device_1.SmartDrop.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isCamera(device.device_type)) {
new_device = device_1.Camera.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isLock(device.device_type)) {
new_device = device_1.Lock.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isMotionSensor(device.device_type)) {
new_device = device_1.MotionSensor.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isEntrySensor(device.device_type)) {
new_device = device_1.EntrySensor.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isKeyPad(device.device_type)) {
new_device = device_1.Keypad.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isSmartSafe(device.device_type)) {
new_device = device_1.SmartSafe.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isSmartTrack(device.device_type)) {
new_device = device_1.Tracker.getInstance(this.api, device, deviceConfig);
}
else if (device_1.Device.isLockKeypad(device.device_type)) {
new_device = device_1.LockKeypad.getInstance(this.api, device, deviceConfig);
}
else {
new_device = device_1.UnknownDevice.getInstance(this.api, device, deviceConfig);
}
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));
device.on("tampering", (device, state) => this.onDeviceTampering(device, state));
device.on("low temperature", (device, state) => this.onDeviceLowTemperature(device, state));
device.on("high temperature", (device, state) => this.onDeviceHighTemperature(device, state));
device.on("pin incorrect", (device, state) => this.onDevicePinIncorrect(device, state));
device.on("lid stuck", (device, state) => this.onDeviceLidStuck(device, state));
device.on("battery fully charged", (device, state) => this.onDeviceBatteryFullyCharged(device, state));
this.addDevice(device);
device.initialize();
}
catch (err) {
const error = (0, error_1.ensureError)(err);
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.debug(`Initiate first station connection to get data over p2p`, { stationSN: station.getSerial() });
station.connect();
}
}).catch((err) => {
const error = (0, error_1.ensureError)(err);
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.error("Error in processing invitations", { error: (0, utils_1.getError)(error) });
});
}
await this.api.refreshAllData().catch(err => {
const error = (0, error_1.ensureError)(err);
logging_1.rootMainLogger.error("Error during API data refreshing", { error: (0, utils_1.getError)(error) });
});
if (this.refreshEufySecurityCloudTimeout !== undefined)
clearTimeout(this.refreshEufySecurityCloudTimeout);
if (this.config.pollingIntervalMinutes > 0)
this.refreshEufySecurityCloudTimeout = setTimeout(() => { this.refreshCloudData(); }, this.config.pollingIntervalMinutes * 60 * 1000);
else
logging_1.rootMainLogger.info(`Automatic retrieval of data from the cloud has been deactivated (config pollingIntervalMinutes: ${this.config.pollingIntervalMinutes})`);
}
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);
logging_1.rootMainLogger.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);
logging_1.rootMainLogger.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 {
logging_1.rootMainLogger.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 {
logging_1.rootMainLogger.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(() => {
logging_1.rootMainLogger.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 {
logging_1.rootMainLogger.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);