homebridge-eufy-security
Version:
Control Eufy Security from homebridge.
146 lines • 5.99 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.is_rtsp_ready = exports.UniversalStream = exports.Deferred = exports.ffmpegLogger = exports.tsLogger = exports.log = exports.CHAR = exports.SERV = exports.HAP = void 0;
exports.setHap = setHap;
exports.init_log = init_log;
const tslog_1 = require("tslog");
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 eufy_security_client_1 = require("eufy-security-client");
function setHap(hapInstance) {
exports.HAP = hapInstance;
exports.SERV = hapInstance.Service;
exports.CHAR = hapInstance.Characteristic;
}
exports.log = {};
exports.tsLogger = {};
exports.ffmpegLogger = {};
function init_log(logOptions) {
exports.log = new tslog_1.Logger(logOptions);
exports.tsLogger = new tslog_1.Logger({ ...logOptions, type: 'hidden' });
exports.ffmpegLogger = new tslog_1.Logger({ ...logOptions, type: 'hidden' });
}
class Deferred {
finished = false;
resolve;
reject;
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 {
url;
static socks = new Set();
server;
sock_id;
isWin32 = false;
startTime = Date.now();
constructor(namespace, onSocket) {
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', (error) => {
// More robust error handling
exports.ffmpegLogger.debug('Server error:', error);
this.close();
})
.listen(sockpath, () => {
exports.ffmpegLogger.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
exports.ffmpegLogger.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) {
exports.ffmpegLogger.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) {
exports.ffmpegLogger.debug(`An error occurred while unlinking the file: ${error}`);
}
}
UniversalStream.socks.delete(this.sock_id);
exports.ffmpegLogger.debug('Resources cleaned up.');
}
}
static StreamInput(namespace, stream) {
return new UniversalStream(namespace, (socket) => stream.pipe(socket, { end: true }));
}
static StreamOutput(namespace, stream) {
return new UniversalStream(namespace, (socket) => socket.pipe(stream, { end: true }));
}
}
exports.UniversalStream = UniversalStream;
const is_rtsp_ready = function (device, cameraConfig) {
exports.log.debug(device.getName(), 'RTSP rtspStream:', device.hasProperty('rtspStream'));
if (!device.hasProperty('rtspStream')) {
exports.log.debug(device.getName(), 'Looks like not compatible with RTSP');
return false;
}
exports.log.debug(device.getName(), 'RTSP cameraConfig: ', cameraConfig.rtsp);
if (!cameraConfig.rtsp) {
exports.log.debug(device.getName(), 'Looks like RTSP is not enabled on camera config');
return false;
}
exports.log.debug(device.getName(), 'RTSP ', device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStream));
if (!device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStream)) {
exports.log.debug(device.getName(), ': RTSP capabilities not enabled. You will need to do it manually!');
return false;
}
exports.log.debug(device.getName(), 'RTSP ', device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStreamUrl));
if (device.getPropertyValue(eufy_security_client_1.PropertyName.DeviceRTSPStreamUrl) === '') {
exports.log.debug(device.getName(), ': RTSP URL is unknow');
return false;
}
return true;
};
exports.is_rtsp_ready = is_rtsp_ready;
//# sourceMappingURL=utils.js.map
;