-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
289 lines (258 loc) · 10.8 KB
/
node_helper.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* MagicMirror²
* Module: MMM-SynologySurveillance
*
* By Tom Hirschberger
* MIT Licensed.
*/
const NodeHelper = require("node_helper")
const MySynoSSClient = require("./MySynoSSClient")
const MjpegDiskStation = require("./MjpegDiskStation")
module.exports = NodeHelper.create({
start: function () {
const self = this
self.lastRefresh = -1
self.started = false
self.urlUpdateInProgress = false
self.ds = []
self.iterationCnt = 0
self.mjpegDs = {}
},
stop: async function(){
const self = this
if (self.config.debug){
console.log(self.name+": Will logout all clients")
}
for (let dsIdx = 0; dsIdx < self.ds.length; dsIdx++){
try{
await self.ds[dsIdx].client.logout()
if (self.config.debug){
console.log(self.name+": Logout of DiskStation with idx: "+dsIdx+" successful")
}
} catch {
if (self.config.debug){
console.log(self.name+": Logout of DiskStation with idx: "+dsIdx+" failed")
}
}
}
},
replaceUrlParts: function(url, newProtocol=null, newHost=null, newPort=null){
let newUrl = new URL(url)
if(newProtocol != null){
newUrl.protocol = newProtocol
}
if(newHost != null){
newUrl.hostname = newHost
}
if(newPort != null){
newUrl.port = newPort
}
return newUrl.toString()
},
getInfoOfAllDs: async function(){
const self = this
console.log(self.name + "Trying to get infos of all "+self.ds.length+" DiskStations!")
for(let dsIdx = 0; dsIdx < self.ds.length; dsIdx++){
if (self.config.debug){
console.log(self.name + "Trying to get infos of ds with idx: "+dsIdx)
}
self.ds[dsIdx].infos = {
camIds: [],
camNameIdMapping: {},
camIdNameMapping: {},
infosPerId: {}
}
try {
if (self.config.showPositions || self.config.showBigPositions) {
self.ds[dsIdx].infos = await self.ds[dsIdx].client.getAllInfosOfAllCams(true)
} else {
self.ds[dsIdx].infos = await self.ds[dsIdx].client.getStreamInfoOfAllCams(true)
}
} catch (getInfosErr) {
if ((typeof getInfosErr.returnCode !== "undefined") && (getInfosErr.returnCode === 105)){
if (self.config.debug){
console.log(self.name + "Got privilege error while fetching the info for DiskStation with idx "+dsIdx+". Trying again without using cached data!")
}
try {
if (self.config.showPositions || self.config.showBigPositions) {
self.ds[dsIdx].infos = await self.ds[dsIdx].client.getAllInfosOfAllCams(false)
} else {
self.ds[dsIdx].infos = await self.ds[dsIdx].client.getStreamInfoOfAllCams(false)
}
} catch (getInfosNoCacheErr) {
console.log(getInfosNoCacheErr)
}
} else {
console.log(getInfosErr)
}
}
if (self.config.debug){
console.log(self.name + "New infos of DiskStation with idx: "+dsIdx)
console.log(JSON.stringify(self.ds[dsIdx].infos,null,2))
}
let newProtocol = null
let newHost = null
let newPort = null
if (self.ds[dsIdx].replaceHostPart){
newProtocol = self.config.ds[dsIdx].protocol
newHost = self.config.ds[dsIdx].host
}
if(self.ds[dsIdx].replacePortPart){
newPort = self.config.ds[dsIdx].port
}
for (let camId of self.ds[dsIdx].infos.camIds){
let curStreamInfo = self.ds[dsIdx].infos.infosPerId[camId].streamInfo
let newStreamInfo = self.replaceUrlParts(
curStreamInfo,
newProtocol,
newHost,
newPort
)
self.ds[dsIdx].infos.infosPerId[camId].streamInfo = newStreamInfo
if (self.config.debug){
console.log(self.name+": CamInfo "+self.ds[dsIdx].infos.camIdNameMapping[camId]+" -> "+self.ds[dsIdx].infos.infosPerId[camId].streamInfo)
}
}
}
self.sendSocketNotification("DS_STREAM_INFO", self.ds)
},
Sleep: function (milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
},
goPosition: async function (dsIdx, camName, position, oldPosition) {
const self = this
if (self.started){
while (self.urlUpdateInProgress) {
self.Sleep(1000);
}
if (typeof self.ds[dsIdx] !== "undefined"){
if(typeof self.ds[dsIdx].infos.camNameIdMapping[camName] !== "undefined"){
let curCamId = self.ds[dsIdx].infos.camNameIdMapping[camName]
if (self.config.debug){
console.log(self.name + ": Id of cam " + camName+" is "+self.ds[dsIdx].infos.camNameIdMapping[camName])
}
if (typeof self.ds[dsIdx].infos.infosPerId[curCamId].presets[position] !== "undefined"){
try {
await self.ds[dsIdx].client.goPTZPosition(curCamId, self.ds[dsIdx].infos.infosPerId[curCamId].presets[position].position, true)
self.sendSocketNotification("DS_CHANGED_POSITION", {dsIdx: dsIdx, camName: camName, position: position})
} catch (goPositionError){
if ((typeof goPositionError.returnCode !== "undefined") && (goPositionError.returnCode === 105)){
if (self.config.debug){
console.log(self.name + "Got privilege error while changing the position of cam: "+camName+" of DiskStation with idx "+dsIdx+". Trying again without using cached data!")
}
try {
await self.ds[dsIdx].client.goPTZPosition(curCamId, self.ds[dsIdx].infos.infosPerId[curCamId].presets[position].position, false)
self.sendSocketNotification("DS_CHANGED_POSITION", {dsIdx: dsIdx, camName: camName, position: position})
} catch (goPositionNoCacheError) {
console.log(goPositionNoCacheError)
if (typeof oldPosition !== "undefined"){
self.sendSocketNotification("DS_CHANGED_POSITION", {dsIdx: dsIdx, camName: camName, position: oldPosition})
}
}
} else {
if (typeof oldPosition !== "undefined"){
self.sendSocketNotification("DS_CHANGED_POSITION", {dsIdx: dsIdx, camName: camName, position: oldPosition})
}
}
}
} else {
console.log(self.name +": Could not change position of cam: "+ camName +" of ds: "+ dsIdx +" because there exists no position with idx "+position+"!")
if (typeof oldPosition !== "undefined"){
self.sendSocketNotification("DS_CHANGED_POSITION", {dsIdx: dsIdx, camName: camName, position: oldPosition})
}
}
} else {
console.log(self.name +": Could not change position of cam: "+ camName +" of ds: "+ dsIdx +" because there exists no information about a cam with this name!")
if (typeof oldPosition !== "undefined"){
self.sendSocketNotification("DS_CHANGED_POSITION", {dsIdx: dsIdx, camName: camName, position: oldPosition})
}
}
}
}
},
socketNotificationReceived: async function (notification, payload) {
const self = this
if ((typeof self.config !== "undefined") && (self.config.debug)) {
console.log(self.name + ": Received notification " + notification)
}
if (notification === "CONFIG" && self.started === false) {
self.config = payload
if ((typeof self.config.ds !== "undefined") && (Array.isArray(self.config.ds))){
if (self.config.debug){
console.log(self.name + "At least one DiskStation is configured")
}
for (let dsIdx = 0; dsIdx < self.config.ds.length; dsIdx++) {
if (self.config.debug){
console.log(self.name + "Prepare datastructures for DiskStation with idx: "+dsIdx)
}
let curDs = {}
let curDsConfig = self.config.ds[dsIdx]
if (curDsConfig.protocol === "mjpeg"){
curDs.client = new MjpegDiskStation(dsIdx, curDsConfig.cams)
curDs.infos = await curDs.client.getAllInfosOfAllCams()
} else {
curDs.infos = {
camIds: [],
camNameIdMapping: {},
camIdNameMapping: {},
infosPerId: {}
}
let opts = {
protocol: curDsConfig.protocol || "http",
host: curDsConfig.host || null,
port: curDsConfig.port || 5000,
ignoreCertErrors: curDsConfig.ignoreCertErrors || true,
user: curDsConfig.user || null,
password: curDsConfig.password || null,
}
if (typeof curDsConfig.debug !== "undefined"){
opts.debug = curDsConfig.debug
} else {
opts.debug = self.config.debug
}
if (opts.debug != true){
opts.debug = false
}
curDs.client = new MySynoSSClient(opts)
if ((typeof curDsConfig.replaceHostPart !== "undefined") && (!curDsConfig.replaceHostPart)) {
curDs.replaceHostPart = false
} else {
curDs.replaceHostPart = true
}
if ((typeof curDsConfig.replacePortPart !== "undefined") && (!curDsConfig.replacePortPart)) {
curDs.replacePortPart = false
} else {
curDs.replacePortPart = true
}
}
self.ds.push(curDs)
}
} else {
console.log(self.name + ": Could not find some DiskStation information in the config (option ds is missing or not an array)!")
}
self.started = true
} else if (notification === "INIT_DS") {
self.lastRefresh = Date.now()
self.urlUpdateInProgress = true
self.getInfoOfAllDs()
self.urlUpdateInProgress = false
} else if (notification === "REFRESH_URLS" && self.started) {
if ((Date.now() - self.lastRefresh) > self.config.minimumTimeBetweenRefreshs){
if (self.config.debug){
console.log(self.name + ": Refreshing the URLs of all DiskStations!")
}
self.lastRefresh = Date.now()
self.urlUpdateInProgress = true
self.getInfoOfAllDs()
self.urlUpdateInProgress = false
}
} else if (notification === "SYNO_SS_CHANGE_CAM") {
self.sendSocketNotification(notification, payload)
} else if (notification === "DS_CHANGE_POSITION") {
console.log(self.name +": Changing position of cam: "+ payload.camName +" of ds: "+ payload.dsIdx +" to: "+ payload.position)
self.goPosition(payload.dsIdx, payload.camName, payload.position, payload.oldPosition)
} else if (notification === "SYNO_INVALIDATE_URL"){
self.iterationCnt = self.config.iterationCnt+10
self.getStreamUrls()
}
}
});