Skip to content

Instantly share code, notes, and snippets.

@OsvaldoFrias
Created February 18, 2021 23:25
Show Gist options
  • Save OsvaldoFrias/79fe179baad2932a1e39a4dd1aa8c756 to your computer and use it in GitHub Desktop.
Save OsvaldoFrias/79fe179baad2932a1e39a4dd1aa8c756 to your computer and use it in GitHub Desktop.
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]);
}
});
@OsvaldoFrias
Copy link
Author

Se actualizó la implementación, se utiliza el proyecto:

https://github.com/ISSIVS/MX_PRJ_Vivotek-IB9365

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment