homebridge-eufy-security-mikebcbc
Version:
Control Eufy Security from homebridge.
129 lines • 5.35 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniversalStream = exports.Deferred = exports.is_rtsp_ready = void 0;
const eufy_security_client_1 = require("eufy-security-client");
const net_1 = __importDefault(require("net"));
const path_1 = __importDefault(require("path"));
const os_1 = require("os");
const fs_extra_1 = __importDefault(require("fs-extra"));
const is_rtsp_ready = function (device, cameraConfig, log) {
log.debug(device.getName(), 'RTSP rtspStream:' + device.hasProperty('rtspStream'));
if (!device.hasProperty('rtspStream')) {
log.debug(device.getName(), 'Looks like not compatible with RTSP');
return false;
}
log.debug(device.getName(), 'RTSP cameraConfig: ' + cameraConfig.rtsp);
if (!cameraConfig.rtsp) {
log.debug(device.getName(), 'Looks like RTSP is not enabled on camera config');
return false;
}
log.debug(device.getName(), 'RTSP ' + device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStream));
if (!device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStream)) {
log.debug(device.getName(), ': RTSP capabilities not enabled. You will need to do it manually!');
return false;
}
log.debug(device.getName(), 'RTSP ' + device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStreamUrl));
if (device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStreamUrl) === '') {
log.debug(device.getName(), ': RTSP URL is unknow');
return false;
}
return true;
};
exports.is_rtsp_ready = is_rtsp_ready;
class Deferred {
constructor() {
this.finished = false;
this.promise = new Promise((resolve, reject) => {
this.resolve = v => {
this.finished = true;
resolve(v);
return this;
};
this.reject = e => {
this.finished = true;
reject(e);
return this;
};
});
}
}
exports.Deferred = Deferred;
class UniversalStream {
constructor(namespace, onSocket, log) {
this.log = log;
this.isWin32 = false;
this.startTime = Date.now();
this.isWin32 = process.platform === 'win32'; // Cache platform check
const unique_sock_id = Math.min(...Array.from({ length: 100 }, (_, i) => i + 1).filter(i => !UniversalStream.socks.has(i)));
UniversalStream.socks.add(unique_sock_id);
this.sock_id = unique_sock_id;
const sockpath = this.generateSockPath(namespace, unique_sock_id);
this.url = this.generateUrl(sockpath);
this.server = net_1.default.createServer(onSocket)
.on('error', (err) => {
// More robust error handling
this.log.debug('Server error:', err);
this.close();
})
.listen(sockpath, () => {
this.log.debug('Server is listening');
});
}
generateSockPath(namespace, unique_sock_id) {
const stepStartTime = Date.now(); // Start time for this step
let sockpath = '';
const pipeName = `${namespace}.${unique_sock_id}.sock`; // Use template literals
if (this.isWin32) {
const pipePrefix = '\\\\.\\pipe\\';
sockpath = path_1.default.join(pipePrefix, pipeName);
}
else {
sockpath = path_1.default.join((0, os_1.tmpdir)(), pipeName);
// Use async file operations
if (fs_extra_1.default.existsSync(sockpath)) {
fs_extra_1.default.unlinkSync(sockpath);
}
}
const stepEndTime = Date.now(); // End time for this step
// eslint-disable-next-line max-len
this.log.debug(`Time taken for generateSockPath: ${stepEndTime - stepStartTime}ms (Total time from start: ${stepEndTime - this.startTime}ms)`);
return sockpath;
}
generateUrl(sockpath) {
return this.isWin32 ? sockpath : `unix:${sockpath}`; // Use template literals
}
close() {
try {
if (this.server) {
this.server.close();
}
}
catch (error) {
this.log.debug(`An error occurred while closing the server: ${error}`);
}
finally {
if (!this.isWin32 && this.url) {
try {
fs_extra_1.default.unlinkSync(this.url.replace('unix:', ''));
}
catch (error) {
this.log.debug(`An error occurred while unlinking the file: ${error}`);
}
}
UniversalStream.socks.delete(this.sock_id);
this.log.debug('Resources cleaned up.');
}
}
static StreamInput(namespace, stream, log) {
return new UniversalStream(namespace, (socket) => stream.pipe(socket, { end: true }), log);
}
static StreamOutput(namespace, stream, log) {
return new UniversalStream(namespace, (socket) => socket.pipe(stream, { end: true }), log);
}
}
exports.UniversalStream = UniversalStream;
UniversalStream.socks = new Set();
//# sourceMappingURL=utils.js.map