2021-11-26 22:54:04 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const { createWebSocketClient } = require('../basic/websocketTools.js')
|
2021-11-25 07:59:13 +00:00
|
|
|
module.exports = function(s,config,lang,app,io){
|
2021-11-25 17:30:23 +00:00
|
|
|
const { cameraDestroy } = require('../monitor/utils.js')(s,config,lang)
|
2021-11-25 17:20:50 +00:00
|
|
|
var checkCpuInterval = null;
|
2021-11-25 07:59:13 +00:00
|
|
|
function onDataFromMasterNode(d) {
|
|
|
|
switch(d.f){
|
|
|
|
case'sqlCallback':
|
|
|
|
const callbackId = d.callbackId;
|
2021-11-25 20:49:35 +00:00
|
|
|
if(s.queuedSqlCallbacks[callbackId]){
|
|
|
|
s.queuedSqlCallbacks[callbackId](d.err,d.rows)
|
|
|
|
delete(s.queuedSqlCallbacks[callbackId])
|
2021-11-25 07:59:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case'init_success':
|
2021-11-27 03:48:59 +00:00
|
|
|
console.error(new Date(),'Child Nodes : Authenticated with Master Node!');
|
2021-11-25 17:20:50 +00:00
|
|
|
s.connectedToMasterNode = true;
|
2021-11-26 22:54:04 +00:00
|
|
|
s.other_helpers = d.child_helpers;
|
|
|
|
s.childNodeIdOnMasterNode = d.connectionId
|
2021-11-25 07:59:13 +00:00
|
|
|
break;
|
|
|
|
case'kill':
|
|
|
|
s.initiateMonitorObject(d.d);
|
|
|
|
cameraDestroy(d.d)
|
|
|
|
break;
|
|
|
|
case'sync':
|
|
|
|
s.initiateMonitorObject(d.sync);
|
|
|
|
Object.keys(d.sync).forEach(function(v){
|
|
|
|
s.group[d.sync.ke].activeMonitors[d.sync.mid][v]=d.sync[v];
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case'delete'://delete video
|
|
|
|
s.file('delete',s.dir.videos+d.ke+'/'+d.mid+'/'+d.file)
|
|
|
|
break;
|
|
|
|
case'deleteTimelapseFrame'://delete video
|
2021-11-26 22:54:04 +00:00
|
|
|
var filePath = s.getTimelapseFrameDirectory(d.d) + `${d.currentDate}/` + d.file
|
2021-11-25 07:59:13 +00:00
|
|
|
s.file('delete',filePath)
|
|
|
|
break;
|
|
|
|
case'cameraStop'://start camera
|
|
|
|
s.camera('stop',d.d)
|
|
|
|
break;
|
|
|
|
case'cameraStart'://start or record camera
|
|
|
|
s.camera(d.mode,d.d)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 17:20:50 +00:00
|
|
|
function initiateConnectionToMasterNode(){
|
|
|
|
s.cx({
|
2021-11-25 17:30:23 +00:00
|
|
|
f: 'init',
|
|
|
|
port: config.port,
|
|
|
|
coreCount: s.coreCount,
|
|
|
|
availableHWAccels: config.availableHWAccels,
|
|
|
|
socketKey: config.childNodes.key
|
2021-11-25 17:20:50 +00:00
|
|
|
})
|
|
|
|
clearInterval(checkCpuInterval)
|
2021-11-25 20:49:35 +00:00
|
|
|
checkCpuInterval = setInterval(sendCurrentCpuUsage,5000)
|
2021-11-25 17:20:50 +00:00
|
|
|
}
|
|
|
|
function onDisconnectFromMasterNode(){
|
|
|
|
s.connectedToMasterNode = false;
|
|
|
|
destroyAllMonitorProcesses()
|
|
|
|
}
|
|
|
|
function destroyAllMonitorProcesses(){
|
|
|
|
var groupKeys = Object.keys(s.group)
|
|
|
|
groupKeys.forEach(function(groupKey){
|
|
|
|
var activeMonitorKeys = Object.keys(s.group[groupKey].activeMonitors)
|
|
|
|
activeMonitorKeys.forEach(function(monitorKey){
|
|
|
|
var activeMonitor = s.group[groupKey].activeMonitors[monitorKey]
|
|
|
|
if(activeMonitor && activeMonitor.spawn && activeMonitor.spawn.close)activeMonitor.spawn.close()
|
|
|
|
if(activeMonitor && activeMonitor.spawn && activeMonitor.spawn.kill)activeMonitor.spawn.kill()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2021-11-25 20:49:35 +00:00
|
|
|
async function sendCurrentCpuUsage(){
|
2021-11-25 17:20:50 +00:00
|
|
|
const cpu = await s.cpuUsage()
|
|
|
|
s.cx({
|
|
|
|
f: 'cpu',
|
|
|
|
cpu: parseFloat(cpu)
|
|
|
|
})
|
|
|
|
}
|
2021-11-27 01:47:20 +00:00
|
|
|
function createFileTransferToMasterNode(filePath,transferInfo,fileType){
|
2021-11-26 22:54:04 +00:00
|
|
|
const response = {ok: true}
|
|
|
|
return new Promise((resolve,reject) => {
|
|
|
|
const fileTransferConnection = createWebSocketClient('ws://'+config.childNodes.host + '/childNodeFileRelay',{
|
|
|
|
onMessage: () => {}
|
|
|
|
})
|
|
|
|
fileTransferConnection.on('open', function(){
|
|
|
|
fileTransferConnection.send(JSON.stringify({
|
2021-11-27 01:47:20 +00:00
|
|
|
fileType: fileType || 'video',
|
|
|
|
options: transferInfo,
|
2021-11-26 22:54:04 +00:00
|
|
|
socketKey: config.childNodes.key,
|
|
|
|
connectionId: s.childNodeIdOnMasterNode,
|
|
|
|
}))
|
|
|
|
setTimeout(() => {
|
|
|
|
fs.createReadStream(filePath,{ highWaterMark: 500 })
|
|
|
|
.on('data',function(data){
|
|
|
|
fileTransferConnection.send(data)
|
|
|
|
})
|
|
|
|
.on('close',function(){
|
|
|
|
fileTransferConnection.close()
|
|
|
|
resolve(response)
|
|
|
|
})
|
|
|
|
},2000)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2021-11-27 01:47:20 +00:00
|
|
|
async function sendVideoToMasterNode(filePath,options){
|
|
|
|
const groupKey = options.ke
|
|
|
|
const monitorId = options.mid
|
|
|
|
const activeMonitor = s.group[groupKey].activeMonitors[monitorId]
|
|
|
|
const response = await createFileTransferToMasterNode(filePath,options,'video');
|
|
|
|
clearTimeout(activeMonitor.recordingChecker);
|
|
|
|
clearTimeout(activeMonitor.streamChecker);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
async function sendTimelapseFrameToMasterNode(filePath,options){
|
|
|
|
const response = await createFileTransferToMasterNode(filePath,options,'timelapseFrame');
|
|
|
|
return response;
|
|
|
|
}
|
2021-11-25 07:59:13 +00:00
|
|
|
return {
|
|
|
|
onDataFromMasterNode,
|
2021-11-25 17:20:50 +00:00
|
|
|
initiateConnectionToMasterNode,
|
|
|
|
onDisconnectFromMasterNode,
|
|
|
|
destroyAllMonitorProcesses,
|
|
|
|
sendCurrentCpuUsage,
|
2021-11-26 22:54:04 +00:00
|
|
|
sendVideoToMasterNode,
|
2021-11-27 01:47:20 +00:00
|
|
|
sendTimelapseFrameToMasterNode,
|
2021-11-25 07:59:13 +00:00
|
|
|
}
|
|
|
|
}
|