homebridge-ezviz
Version:
EZVIZ plugin for homebridge: https://homebridge.io/
173 lines • 7.49 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EAVIZAccessory = void 0;
const streaming_delegate_1 = require("./streaming-delegate");
const camera_1 = require("./ezviz/models/camera");
const sanitizeString = (str) => {
if (str.includes('package')) {
return str.replace('-', ' ').replace(/(?:^|\s|["'([{])+\S/g, (match) => match.toUpperCase());
}
else if (str.startsWith('Face') || str.startsWith('Zone')) {
return str;
}
else {
return str.replace(/(?:^|\s|["'([{])+\S/g, (match) => match.toUpperCase());
}
};
class EAVIZAccessory {
constructor(accessory, camera, config, log, hap) {
this.accessory = accessory;
this.camera = camera;
this.config = config;
this.log = log;
this.hap = hap;
}
createService(serviceType, name) {
const existingService = name
? this.accessory.getServiceById(serviceType, `${this.accessory.displayName} ${name}`)
: this.accessory.getService(serviceType);
const service = existingService ||
(name
? this.accessory.addService(serviceType, `${this.accessory.displayName} ${name}`, `${this.accessory.displayName} ${name}`)
: this.accessory.addService(serviceType, this.accessory.displayName));
return service;
}
removeService(serviceType, name) {
const existingService = name
? this.accessory.getServiceById(serviceType, `${this.accessory.displayName} ${name}`)
: this.accessory.getService(serviceType);
if (existingService) {
this.accessory.removeService(existingService);
}
}
removeAllServicesByType(serviceType) {
let existingService = this.accessory.getService(serviceType);
while (existingService) {
this.accessory.removeService(existingService);
existingService = this.accessory.getService(serviceType);
}
}
createSwitchService(name, serviceType, type, cb) {
const service = this.createService(serviceType, name);
this.log.debug(`Creating switch for ${this.accessory.displayName} ${name}.`);
const switchSwitch = this.camera.info.switch.find((x) => x.type === type);
if (switchSwitch) {
service
.setCharacteristic(this.hap.Characteristic.On, switchSwitch.enable)
.getCharacteristic(this.hap.Characteristic.On)
.on("set", (value, callback) => __awaiter(this, void 0, void 0, function* () {
cb(value);
this.log.info(`Setting ${this.accessory.displayName} ${name} to ${value ? 'on' : 'off'}`);
callback();
}))
.on("get", (callback) => __awaiter(this, void 0, void 0, function* () {
const info = yield this.camera.updateData();
this.accessory.context.cameraInfo = info;
const ss = info.switch.find((x) => x.type === type);
if (ss) {
const value = ss.enable;
if (typeof value !== 'undefined') {
this.log.debug(`Updating info for ${this.accessory.displayName} ${name}`);
callback(null, value);
}
else {
callback(new Error(), undefined);
}
}
}));
}
}
configureController() {
const streamingDelegate = new streaming_delegate_1.StreamingDelegate(this.hap, this.camera, this.config, this.log);
const options = {
cameraStreamCount: 2,
delegate: streamingDelegate,
streamingOptions: {
supportedCryptoSuites: [0],
video: {
resolutions: [
[320, 180, 30],
[320, 240, 15],
[320, 240, 30],
[480, 270, 30],
[480, 360, 30],
[640, 360, 30],
[640, 480, 30],
[1280, 720, 30],
[1280, 960, 30],
[1920, 1080, 30],
[1600, 1200, 30],
],
codec: {
profiles: [0, 1, 2],
levels: [0, 1, 2],
},
},
audio: {
twoWayAudio: false,
codecs: [
{
type: "AAC-eld",
samplerate: 16,
},
],
},
},
};
const cameraController = new this.hap.CameraController(options);
streamingDelegate.controller = cameraController;
this.accessory.configureController(cameraController);
}
getServicesByType(serviceType) {
return this.accessory.services.filter((x) => x.UUID === serviceType.UUID);
}
toggleSleep(enabled) {
return __awaiter(this, void 0, void 0, function* () {
const service = this.accessory.getService(`${this.accessory.displayName} Sleep Mode`);
let value = 1;
if (!enabled) {
value = 0;
}
const set = yield this.camera.setSwitchProperty(camera_1.SwitchTypes.Sleep, value);
if (set && service) {
service.updateCharacteristic(this.hap.Characteristic.On, enabled);
}
});
}
toggleAudio(enabled) {
return __awaiter(this, void 0, void 0, function* () {
const service = this.accessory.getService(`${this.accessory.displayName} Audio`);
let value = 1;
if (!enabled) {
value = 0;
}
const set = yield this.camera.setSwitchProperty(camera_1.SwitchTypes.Audio, value);
if (set && service) {
service.updateCharacteristic(this.hap.Characteristic.On, enabled);
}
});
}
setMotion(state, types) {
if (this.hap) {
types.forEach((type) => {
type = sanitizeString(type);
const service = this.accessory.getServiceById(this.hap.Service.MotionSensor, `${this.accessory.displayName} ${type}`);
if (service) {
this.log.debug(`Setting ${this.accessory.displayName} ${type} Motion to ${state}`);
service.updateCharacteristic(this.hap.Characteristic.MotionDetected, state);
}
});
}
}
}
exports.EAVIZAccessory = EAVIZAccessory;
//# sourceMappingURL=accessory.js.map