@koush/ring-client-api
Version:
Unofficial API for Ring doorbells, cameras, security alarm system and smart lighting
36 lines (35 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSystemId = exports.updateHomebridgeConfig = void 0;
const fs_1 = require("fs");
const crypto_1 = require("crypto");
const path_1 = require("path");
const systemIdFileName = '.ring.json';
function updateHomebridgeConfig(homebridge, update) {
const configPath = homebridge.user.configPath(), config = (0, fs_1.readFileSync)(configPath).toString(), updatedConfig = update(config);
if (config !== updatedConfig) {
(0, fs_1.writeFileSync)(configPath, updatedConfig);
return true;
}
return false;
}
exports.updateHomebridgeConfig = updateHomebridgeConfig;
function createSystemId() {
return (0, crypto_1.createHash)('sha256').update((0, crypto_1.randomBytes)(32)).digest('hex');
}
function getSystemId(homebridge) {
const storagePath = homebridge.user.storagePath(), filePath = (0, path_1.join)(storagePath, systemIdFileName);
try {
const ringContext = JSON.parse((0, fs_1.readFileSync)(filePath).toString());
if (ringContext.systemId) {
return ringContext.systemId;
}
}
catch (_) {
// expect errors if file doesn't exist or is in a bad format
}
const systemId = createSystemId(), ringContext = { systemId };
(0, fs_1.writeFileSync)(filePath, JSON.stringify(ringContext));
return systemId;
}
exports.getSystemId = getSystemId;