//! IMPORTANT /** * THIS IS JUST STATIC CODE FOR TEST PURPOSES OF CAMERA IN THE LAB. * IT IS USED TO SEND ACTUAL NUMBER OF PEOPLE IN THE ZONE. ZONE IS CREATED IN datafromsky SOFTWARE * DYNAMIC CODE UNDERNEATH FOR CLIENT PURPOSE * IN DYNAMIC CODE IT IS NECCESSARY TO ADD "SINK_ID CALCULATION" (IN OUR CASE IT IS SET TO "id": 3 AT THE MOMENT) * FOR STATIC CODE, YOU NEED TO SET ACCESS_TOKEN, CUBES_ID, ANALYTICS_ID, SEQUENCE_NUMBER AND SINK_ID MANUALLY * IN OUR CASE NOW IT IS "VTLMQILFQG, 3, 0, 38, 3" * * ENABLE ON LINE 55: process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0 */ // exports.id = 'rce_modul'; // exports.title = 'RCE Modul'; // exports.group = 'Worksys'; // exports.color = '#5D9CEC'; // exports.version = '1.0.1'; // exports.output = ['red', 'white']; // exports.author = 'Rastislav Kovac'; // exports.icon = 'cloud-upload'; // exports.html = ` //
//
//
//
RCE MODUL

//
//
//
//
//
@(User)
//
//
//
@(Password)
//
//
//
//
//
@(IP address)
//
//
//
@(Port)
//
//
//
@(My edge)
//
//
//
// `; // exports.readme = `RCE modul`; // //disabling ssl certification // //process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0 // exports.install = function(instance) { // let startRequests; // let ip; // let port; // let counter; // let previousValue; // let actualDataBody = // { // "sequence_number": "38", // "sinks": [ // { // "id": 3 // } // ] // } // actualDataBody = JSON.stringify(actualDataBody); // let dataHeaders = { // "Content-Type": "application/json", // "Authorization": "Bearer VTLMQILFQG", // "Accept-Version": 3.17, // } // const getActualData = function() { // U.request(dataUrl, ["post"], actualDataBody , function(err, data, status, headers, host) { // if (status !== 200) { // counter++; // } // if (counter > 100 && counter < 102) { // instance.reconfigure(); // } // if (counter > 500){ // instance.send(0, "Error, data are not being updated"); // counter = 0; // } // let response = JSON.parse(data); // let value = response.sinks[0].data.value // let dataToTB = { // 'tbName': [ // { // "ts": Date.now(), // "values": { // "event_count": value, // "event_description":"Pocet osob v miestnosti" // } // } // ] // }; // if (value !== previousValue){ // instance.send(1, dataToTB); // console.log(dataToTB.tbName[0]); // } // previousValue = value; // }, {}, dataHeaders); // } // instance.reconfigure = function() { // clearInterval(startRequests); // options = instance.options; // can = options.username && options.userpassword && options.edge && options.ip? true : false; // instance.status(can ? 'Gathering data' : 'Not configured', can ? 'green' : 'red'); // ip = options.ip; // port = options.port; // dataUrl = `https://${ip}:${port}/cubes/3/analytics/0/sinks/data`; // if (!can) // return; // startRequests = setInterval(getActualData, 1000); // } // instance.on('options', instance.reconfigure); // setTimeout(instance.reconfigure, 3000); // } //! DYNAMIC CODE, ALL AUTOMATIC exports.id = 'rce_modul'; exports.title = 'RCE Modul'; exports.group = 'Worksys'; exports.color = '#5D9CEC'; exports.version = '1.0.2'; exports.output = ['red', 'white']; exports.author = 'Rastislav Kovac'; exports.icon = 'cloud-upload'; exports.html = `
RCE MODUL

@(User)
@(Password)
@(IP address)
@(Port)
@(My edge)
`; exports.readme = `RCE modul`; //disabling ssl certification process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0 exports.install = function(instance) { let startRequests; let ip, port, username, password; let accessToken = "VTLMQILFQG"; let tokenBody; let allCubes = [0,1,2]; // tree cameras let cubeId; let analyticsId; let sequence_number; let counter = 0; let previousValue; let previousValues = {}; let sinksUrlArray; let dataUrlArray; // all urls let tokenUrl, cubesUrl, analyticsUrl, sinksUrl, dataUrl; let actualDataBody; let dataHeaders; // all headers // cubesHeaders === analyticsHeaders === dataHeaders const tokenHeaders = { "Content-Type": "application/json", "Accept-Version": 3.17, } dataHeaders = { "Content-Type": "application/json", "Authorization": `Bearer ${accessToken}`, "Accept-Version": 3.17, } // all body // tokenBody === cubesBody === analyticsBody // const getToken = function() { // console.log("------ url",tokenUrl) // U.request(tokenUrl, ["post"], tokenBody, function(err, data, status, headers, host) { // console.log('tokendata',data, err, status, headers, host); // if (status !== 200) { // counter++; // } // if (counter > 50 && counter < 52) { // instance.reconfigure(); // instance.debug(0, exports.title + " Somethimg is going wrong, reconfiguring ..."); // } // if (counter > 100){ // instance.send(0, exports.title + " Error, data are not being updated"); // instance.debug(0, exports.title + " Error, data are not being updated"); // counter = 0; // } // const response = JSON.parse(data); // console.log("ii--ii", response); // //accessToken = response.access_tokens[0]; // console.log(accessToken); // dataHeaders = { // "Content-Type": "application/json", // "Authorization": `Bearer ${accessToken}`, // "Accept-Version": 3.17, // } // getCubes(); // }, {}, tokenHeaders); // } let cubes = { "cubes": [ { "id": 0, "name": "camera 1" }, { "id": 1, "name": "Camera 2" }, { "id": 2, "name": "Camera 3" } ] } // const getCubes = function() { // U.request(cubesUrl, ["get"], tokenBody, function(err, data, status, headers, host) { // //console.log("getcubes data",data, err, status, headers, host); // if (status !== 200) { // counter++; // } // const response = JSON.parse(data); // console.log("getcubes parsed data",response); // // cubesId = response.cubes[0].id; // response.cubes.map( cube => { // allCubes.push(cube.id); // }) // allCubes.map(cubesId => { // analyticsUrl = `https://${ip}:${port}/cubes/${cubesId}/analytics`; // getAnalytics(); // }) // }, {}, dataHeaders); // } // let sinksUrlArray = [ // `https://${ip}:${port}/cubes/0/analytics/0/sinks`, // `https://${ip}:${port}/cubes/1/analytics/0/sinks`, // `https://${ip}:${port}/cubes/2/analytics/0/sinks`, // ]; // const getAnalytics = function() { // U.request(analyticsUrl, ["get"], tokenBody, function(err, data, status, headers, host) { // //console.log("getAnalytics", data, err, status, headers, host); // if (status !== 200) { // counter++; // } // //console.log("rrrrr", err, data, status, headers, host); // let response = JSON.parse(data); // //console.log("response", response); // //sequence_number = response.analytics[0].sequence_number; // analyticsId = response.analytics[0].id; // //console.log("analyticsid", analyticsId); // sinksUrl = `https://${ip}:${port}/cubes/${cubesId}/analytics/${analyticsId}/sinks`; // //getActualData(); // getSinks(); // }, {}, dataHeaders); // } // let dataUrlArray = [ // `https://${ip}:${port}/cubes/0/analytics/0/sinks/data`, // `https://${ip}:${port}/cubes/1/analytics/0/sinks/data`, // `https://${ip}:${port}/cubes/2/analytics/0/sinks/data`, // ] const mapSinks = function() { sinksUrlArray.map((sinksUrl, index) => { getSinks(sinksUrl, index); }) } const getSinks = function(sinksUrl, index) { U.request(sinksUrl, ["get"], tokenBody, function(err, data, status, headers, host) { console.log("getSinks", data, err, status, headers, host); if (status !== 200) { counter++; } //console.log("rrrrr", err, data, status, headers, host); let response = JSON.parse(data); //console.log("getSinks response", response); sequence_number = response.sequence_number; actualDataBody = { "sequence_number": sequence_number, "sinks": [] }; // dataUrl = `https://${ip}:${port}/cubes/${cubesId}/analytics/${analyticsId}/sinks/data`; dataUrl = dataUrlArray[index]; response.sinks.map( item => { actualDataBody.sinks.push({"id": item.id}) }) console.log("actualdatabody", index, actualDataBody) actualDataBody = JSON.stringify(actualDataBody); //console.log("actualDataBody", actualDataBody); startRequests = setInterval(getActualData, 30000, dataUrl, actualDataBody); }, {}, dataHeaders); } let lavyCrossWalk = "A90zgZKXj6nmEMNrpBkX4Ek48y1ReLva3Vbxwqod"; let outOfTrebicR = "oAVmg6jdlZ0bqKN8Ln70wLkM3WBvRyEeax91OY42" const getActualData = function(dataUrl, actualDataBody) { U.request(dataUrl, ["post"], actualDataBody , function(err, data, status, headers, host) { //console.log(data, err, status, headers, host); if (status !== 200) { //counter++; } if(counter > 20) { clearInterval(startRequests); instance.debug(0, exports.title + " 'getActualData function' Error, data are not being updated"); } let response = JSON.parse(data); let actualTime = Date.now(); let values = {}; if(dataUrl == `https://${ip}:${port}/cubes/0/analytics/0/sinks/data`) { console.log("prva kamera") response.sinks.map(item => { console.log("getActualData", item.id); console.log("getActualData", item); if(item.name == 'Prechod-positive') { values["people_passing_left"] = item.data.value; } if(item.name == 'Prechod-negative') { values["people_passing_right"] = item.data.value; } if(item.name == 'chodci-zona-l') { values["people_left"] = item.data.value; } if(item.name == 'chodci-zona-r') { values["people_right"] = item.data.value; } previousValues[item.name] = item.data.value; }) let dataToTB = { "Lb6wB2EDpqr1jJdx0R58g07ly9KPoAN3GO4egmvM": [ { "ts": actualTime, "values": values } ] } // "people_left" // "people_right" // "aggregation_per_5minutes" // "aggregation_per_15minutes" } if(dataUrl == `https://${ip}:${port}/cubes/1/analytics/0/sinks/data`) { // console.log("druha kamera") // console.log() // response.sinks.map(item => { // console.log("getActualData", item.id); // }) } if(dataUrl == `https://${ip}:${port}/cubes/2/analytics/0/sinks/data`) { // console.log("tretia kamera") // response.sinks.map(item => { // console.log("getActualData", item.id); // console.log("getActualData", item.data); // }) } // let value = response.sinks[0].data.value // let dataToTB = { // 'tbName': [ // { // "ts": Date.now(), // "values": { // "event_count": value, // "event_description":"Pocet osob v miestnosti" // } // } // ] // }; // if (value !== previousValue){ // instance.send(1, dataToTB); // console.log(dataToTB.tbName[0]); // } // previousValue = value; }, {}, dataHeaders); } instance.reconfigure = function() { clearInterval(startRequests); options = instance.options; username = options.username; password = options.userpassword; ip = options.ip; port = options.port; edge = options.edge; can = username && password && edge && ip && port? true : false; instance.status(can ? 'Gathering data' : 'Not configured', can ? 'green' : 'red'); if (!can) return; tokenUrl = `https://${ip}:${port}/users/auth`; cubesUrl = `https://${ip}:${port}/cubes`; tokenBody = { "username": username, "password": password } tokenBody = JSON.stringify(tokenBody); //console.log("tokenBody", tokenBody); sinksUrlArray = [ `https://${ip}:${port}/cubes/0/analytics/0/sinks`, `https://${ip}:${port}/cubes/1/analytics/0/sinks`, `https://${ip}:${port}/cubes/2/analytics/0/sinks`, ]; dataUrlArray = [ `https://${ip}:${port}/cubes/0/analytics/0/sinks/data`, `https://${ip}:${port}/cubes/1/analytics/0/sinks/data`, `https://${ip}:${port}/cubes/2/analytics/0/sinks/data`, ] //startRequests = setInterval(getToken, 10000); setTimeout(mapSinks, 5000); //getToken(); } instance.on('options', instance.reconfigure); setTimeout(instance.reconfigure, 5000); }