forked from homebridge-plugins/homebridge-camera-ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·136 lines (111 loc) · 4.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Declare Variables
var Accessory, Service, Characteristic, hap, UUIDGen;
// Declare Requirements
var SwannCamera = require("./ffmpeg").SwannCamera;
// Declare Exports
module.exports = function(homebridge) {
// Get Variables from HAP
Accessory = homebridge.platformAccessory;
hap = homebridge.hap;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
// Register Platform
homebridge.registerPlatform("homebridge-camera-swann", "Camera-Swann", swannCameraPlatform, true);
}
// Swann Camera Platform
function swannCameraPlatform(log, config, api) {
// Declare Self
var self = this;
// Logging and Config
self.log = log;
self.config = config || {};
// Check API
if (api) {
self.api = api;
if (api.version < 2.1) {
throw new Error("Unexpected API version.");
}
// Finish Launching
self.api.on("didFinishLaunching", self.didFinishLaunching.bind(this));
}
}
// Configure Accessory
swannCameraPlatform.prototype.configureAccessory = function(accessory) {
// Won't be invoked
}
// Finish Launching
swannCameraPlatform.prototype.didFinishLaunching = function() {
// Declare Self, videoProcessor and interfaceName
var self = this;
var videoProcessor = self.config.videoProcessor || "ffmpeg";
var interfaceName = self.config.interfaceName || "";
// Camera Configuration
if (self.config.cameras) {
// List of Configured Accessories
var configuredAccessories = [];
// Loop Through Cameras
var cameras = self.config.cameras;
cameras.forEach(function(cameraConfig) {
// Camera Name and Video Config
var cameraName = cameraConfig.name;
var videoConfig = cameraConfig.videoConfig;
// Check for Parameters
if (!cameraName || !videoConfig) {
self.log("Missing parameters.");
return;
}
// Register Camera Accessory
var uuid = UUIDGen.generate(cameraName);
var cameraAccessory = new Accessory(cameraName, uuid, hap.Accessory.Categories.CAMERA);
var cameraAccessoryInfo = cameraAccessory.getService(Service.AccessoryInformation);
if (cameraConfig.manufacturer) {
cameraAccessoryInfo.setCharacteristic(Characteristic.Manufacturer, cameraConfig.manufacturer);
}
if (cameraConfig.model) {
cameraAccessoryInfo.setCharacteristic(Characteristic.Model, cameraConfig.model);
}
if (cameraConfig.serialNumber) {
cameraAccessoryInfo.setCharacteristic(Characteristic.SerialNumber, cameraConfig.serialNumber);
}
if (cameraConfig.firmwareRevision) {
cameraAccessoryInfo.setCharacteristic(Characteristic.FirmwareRevision, cameraConfig.firmwareRevision);
}
cameraAccessory.context.log = self.log;
// Register Motion Sensor Switch if Required
if (cameraConfig.motion) {
var button = new Service.Switch(cameraName);
cameraAccessory.addService(button);
var motion = new Service.MotionSensor(cameraName);
cameraAccessory.addService(motion);
button.getCharacteristic(Characteristic.On)
.on("set", _Motion.bind(cameraAccessory));
}
// Register Camera Source
var cameraSource = new SwannCamera(hap, cameraConfig, self.log, videoProcessor, interfaceName);
cameraAccessory.configureCameraSource(cameraSource);
configuredAccessories.push(cameraAccessory);
});
// Publish Accessories
self.api.publishCameraAccessories("homebridge-camera-swann", configuredAccessories);
}
};
// Motion Sensor
function _Motion(on, callback) {
// Log Motion
this.context.log("Setting %s Motion to %s", this.displayName, on);
// Detect Motion
this.getService(Service.MotionSensor).setCharacteristic(Characteristic.MotionDetected, (on ? 1 : 0));
if (on) {
setTimeout(_Reset.bind(this), 5000);
}
// Callback
callback();
}
// Reset
function _Reset() {
// Log Reset
this.context.log("Setting %s Button to false", this.displayName);
// Reset Switch
this.getService(Service.Switch).setCharacteristic(Characteristic.On, false);
}