Created
February 18, 2021 23:25
-
-
Save OsvaldoFrias/79fe179baad2932a1e39a4dd1aa8c756 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const securos = require('securos'); | |
// let vivotekCam = { | |
// url: "ws://192.168.15.15:888", | |
// id: "7", | |
// totalIn: 0, | |
// totalOut: 0 | |
// }; | |
let vivotekCams = [ | |
{ | |
url: "ws://192.168.15.15:888", | |
id: "7", | |
totalIn: 0, | |
totalOut: 0 | |
} | |
]; | |
securos.connect(function (core) { | |
for (let i = 0; i < vivotekCams.length; i++) { | |
(async (vivotekCam) => { | |
let camObj = await core.getObject("CAM", vivotekCam.id); | |
// function accumulateCount(acc, countingInfo) { | |
// acc.totalIn += countingInfo['In']; | |
// acc.totalOut += countingInfo['Out']; | |
// return acc; | |
// } | |
function createVivotekComment(camSecuros, vivotek) { | |
let comment = ''; | |
comment = `Cámara ${camSecuros.name} `; | |
comment += `Total de Entrada: ${vivotek.totalIn} `; | |
comment += `Total de Salida: ${vivotek.totalOut} `; | |
return comment; | |
} | |
function sendVcaEvent(camId, comment) { | |
let params = { | |
comment | |
}; | |
core.sendEvent('CAM', camId, "VCA_EVENT", params); | |
} | |
let WebSocketClient = require("websocket").client; | |
let client = new WebSocketClient(); | |
let connected = false; | |
function retryConnection() { | |
if (!connected) { | |
console.log("Retrying connection"); | |
client.connect(vivotekCam.url); | |
} | |
} | |
client.on("connectFailed", function (error) { | |
console.log("Connect Error: " + error.toString()); | |
connected = false; | |
retryConnection(); | |
}); | |
function* processMessage(message) { | |
if (message.type === "utf8") { | |
let data = JSON.parse(message.utf8Data); | |
if (data['Tag'] === 'Event') { | |
console.log("Received: '" + message.utf8Data + "'"); | |
for (let idx in data['Data']) { | |
let eventData = data['Data'][idx]; | |
for (let jdx in eventData['CountingInfo']) { | |
yield eventData['CountingInfo'][jdx]; | |
} | |
} | |
} | |
} | |
} | |
client.on("connect", function (connection) { | |
console.log("WebSocket Client Connected"); | |
connected = true; | |
connection.on("error", function (error) { | |
console.log("Connection Error: " + error.toString()); | |
connected = false; | |
retryConnection(); | |
}); | |
connection.on("close", function () { | |
console.log("echo-protocol Connection Closed"); | |
}); | |
connection.on("message", function (message) { | |
connected = true; | |
const iterator = processMessage(message); | |
while (countingInfo = iterator.next().value) { | |
// vivotekCam = accumulateCount(vivotekCam, countingInfo); | |
vivotekCam.totalIn += countingInfo['In']; | |
vivotekCam.totalOut += countingInfo['Out']; | |
let comment = createVivotekComment(camObj, vivotekCam); | |
sendVcaEvent(vivotekCam.id, comment); | |
} | |
}); | |
}); | |
client.connect(vivotekCam.url); | |
})(vivotekCams[i]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Se actualizó la implementación, se utiliza el proyecto:
https://github.com/ISSIVS/MX_PRJ_Vivotek-IB9365