forked from tasict/homebridge-QNAP-QVRPro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·177 lines (105 loc) · 4.06 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var Accessory, hap, UUIDGen;
var QVRPro = require('./QVRPro').QVRPro;
var request = require("request");
var et = require('elementtree');
var pollingtoevent = require("polling-to-event");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;
hap = homebridge.hap;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform("homebridge-QNAP-QVRPro", "Camera-QNAP-QVRPro", QVRProPlatform, true);
}
function fetchSID(ip, port, ssl, user, password, callback){
var sid = "";
var url = (ssl?"https://":"http://") + ip + ":" + port + "/cgi-bin/authLogin.cgi?user=" + user + "&pwd=" + encodeURIComponent(password) + "&service=1";
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var etree = et.parse(body);
sid = etree.findtext('authSid') ? etree.findtext('authSid') : "";
}
callback(sid);
});
}
function keepSIDAlive(QVRProConfig, callback){
var ip = QVRProConfig.ip;
var port = QVRProConfig.port;
var ssl = QVRProConfig.sslOn || false;
var url = (ssl?"https://":"http://") + ip + ":" + port + "/cgi-bin/authLogin.cgi?sid=" + QVRProConfig.sid;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("keep sid:" + QVRProConfig.sid);
}
else {
fetchSID(ip, port, ssl, user, password, function (sid) {
callback(sid);
});
}
});
}
function fetchCameras (ip, port, ssl, sid, callback){
var cameras = [];
request((ssl?"https://":"http://") + ip + ":" + port + "/qvrpro/apis/camera_status.cgi?x-apima-key=%40APIMA_KEY%40&sid=" + sid +"&act=get_all_status", function (error, response, body) {
if (!error && response.statusCode == 200) {
JSON.parse(body).datas.forEach(function(cameraConfig) {
var guid = cameraConfig.name;
var name = cameraConfig.guid;
if (guid && name) {
cameras.push(cameraConfig);
}
});
}
callback(cameras);
});
}
function QVRProPlatform(log, config, api) {
var self = this;
self.log = log;
self.config = config || {};
if (api) {
self.api = api;
if (api.version < 2.1) {
throw new Error("Unexpected API version.");
}
self.api.on('didFinishLaunching', self.didFinishLaunching.bind(this));
}
}
QVRProPlatform.prototype.configureAccessory = function(accessory) {
// Won't be invoked
}
QVRProPlatform.prototype.didFinishLaunching = function() {
var self = this;
var videoProcessor = self.config.videoProcessor || 'ffmpeg';
if (self.config.server && self.config.server.ip && self.config.server.port && self.config.server.user && self.config.server.password) {
var configuredAccessories = [];
var cameras = [];
var QVRProConfig = self.config.server;
var ip = QVRProConfig.ip;
var port = QVRProConfig.port;
var user = QVRProConfig.user;
var password = new Buffer(QVRProConfig.password).toString('base64');
var ssl = QVRProConfig.sslOn || false;
fetchSID(ip, port, ssl, user, password, function (sid) {
self.log("sid:" + sid);
if(sid.length > 0){
QVRProConfig.sid = sid;
var keepAlive = setInterval( function() {
keepSIDAlive(QVRProConfig, function(sid){
QVRProConfig.sid = sid;
});
}, 30000 );
fetchCameras(ip, port, ssl, QVRProConfig.sid, function (cameras){
cameras.forEach(function(camera) {
var uuid = UUIDGen.generate(camera.guid);
var cameraAccessory = new Accessory(camera.name, uuid, hap.Accessory.Categories.CAMERA);
var cameraSource = new QVRPro(hap, QVRProConfig, camera, self.log);
cameraAccessory.configureCameraSource(cameraSource);
configuredAccessories.push(cameraAccessory);
self.log("Found Camera:" + camera.name);
});
self.api.publishCameraAccessories("Camera-QNAP-QVRPro", configuredAccessories);
});
}
});
}
}