Skip to content

Commit

Permalink
Changes to adopt V2
Browse files Browse the repository at this point in the history
  • Loading branch information
vseenivasan committed Oct 11, 2023
1 parent 4a64e0f commit fefc9db
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 98 deletions.
3 changes: 2 additions & 1 deletion app/controllers/dnc.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ exports.getDevices = (req, res) => {

var options = {
url: constants.DNC_URL+"gdevices",
method: 'POST', // Don't forget this line
method: 'POST',
headers: {'Content-Type': 'application/json' },
form: {'cname':req.query.client}
};

request(options, function(error,resp) {
if(error)
{
Expand Down
58 changes: 21 additions & 37 deletions app/controllers/influx.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ exports.readTvals = (indict) => {
exports.readInflux = (indata) => {
return new Promise(function(resolve, reject) {

const count = indata.id;

var paramarr = indata.sdata.split(",")

var selparam = ""
for(let i=0; i<paramarr.length; i++){
selparam = selparam + indata.aggfn+"("+paramarr[i]+")"
Expand All @@ -253,43 +250,30 @@ exports.readInflux = (indata) => {
}

let devid = "("
orflg = 0

if(indata.device.deviceid != "")

if(indata.device != "")
{
devid = devid+"deviceid+=+'"+indata.device.deviceid+"'"
orflg = 1
devid = devid+indata.type+"+=+'"+indata.device+"')"
}
if(indata.device.devID != "")
{
if(orflg)
{
devid = devid+"+or+"
}
devid = devid+"devID+=+'"+indata.device.devID+"'"
orflg = 1

try{
var fmdtstr = new Date(indata.fmdate)
fmdtstr = fmdtstr.toISOString();
var todtstr = new Date(indata.todate)
todtstr = todtstr.toISOString();
}
if(indata.device.devEUI != "")
{
if(orflg)
{
devid = devid+"+or+"
}
devid = devid+"devEUI+=+'"+indata.device.devEUI+"'"
catch(err){
console.log("Date conversion error: ", err)
}
devid = devid + ")"


var fmdtstr = indata.fmdate.toISOString();
var todtstr = indata.todate.toISOString();

query = ""+indata.server+"/query?db="+indata.db+
"&q=select+"+selparam+"+"+indata.math+"+from+"+
"\""+indata.measure+"\""+"+where+"+devid+"+and+time+>=+'"+fmdtstr+
"'+and+time+<=+'"+todtstr+"'+group+by+time("+indata.gbt+"m)"

query = ""+indata.db.dburl+"/query?db="+indata.db.dbname+
"&q=select+"+selparam+"+"+indata.math+"+from+"+
"\""+indata.db.mmtname+"\""+"+where+"+devid+"+and+time+>=+'"+fmdtstr+
"'+and+time+<=+'"+todtstr+"'+group+by+time("+indata.gbt+"m)"

request.get(query,
{'auth': {'user': indata.user, 'pass': indata.pass, 'sendImmediately': false } },
{'auth': {'user': indata.db.uname, 'pass': indata.db.pwd, 'sendImmediately': false } },
function(error, response)
{
if(error)
Expand All @@ -309,14 +293,14 @@ exports.readInflux = (indata) => {
if(resobj.hasOwnProperty("series"))
{
var resdict = {};
resdict["id"] = count;
// resdict["id"] = count;
resdict["Columns"] = resobj.series[0].columns;
resdict["Values"] = resobj.series[0].values;
resolve(resdict);
}
else
{
console.log("Error-2")
console.log("Error-2: ", resobj)
reject("error");
}
}
Expand All @@ -327,11 +311,11 @@ exports.readInflux = (indata) => {
}
}
catch(err){
console.log("Error-4")
console.log("Error-4: ", err)
reject("error");
}

}
});
});
})
}
4 changes: 2 additions & 2 deletions app/controllers/keys.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ exports.getmeas = async (req, res) => {
}

exports.getfields = async (req, res) => {

if(!req.query.client)
{
return res.status(400).send({
Expand All @@ -83,7 +83,7 @@ exports.getfields = async (req, res) => {

var options = {
url: constants.DNC_URL+"gfields",
method: 'POST', // Don't forget this line
method: 'POST',
headers: {'Content-Type': 'application/json' },
form: {'cname':req.query.client}
};
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/login.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const constants = require('../misc/constants');

exports.getLogin = (req, res) => {
const { uname, pwd } = req.query;

if(!uname || !pwd)
{
res.status(406).send("input data missing");
Expand All @@ -42,15 +42,15 @@ exports.getLogin = (req, res) => {
headers: {'Content-Type': 'application/json' },
form: {'uname':uname,'pwd':pwd }
};

request(options, function(error, resp) {
if(error)
{
console.log("Login Error: ", error)
res.status(500).send('connect to DNC Server failed!');
}
else
{
//console.log("Reply received from Plugin")
if(resp.statusCode == 200)
{
req.apidata = resp.body;
Expand Down
198 changes: 144 additions & 54 deletions app/controllers/sread.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const readsens = require('./influx.controller.js');
const { response } = require('express');
const constants = require('../misc/constants.js');

exports.readData = (req, res) => {
exports.readData = async (req, res) => {
if(!req.query.client || !req.query.sdata || !req.query.device || !req.query.gbt ||
!req.query.fmdate || !req.query.todate || !req.query.aggfn) {
return res.status(400).send({
Expand Down Expand Up @@ -78,13 +78,17 @@ exports.readData = (req, res) => {
// Read HWID of the selected DNC Tags from deviceCID collection
// Read the corresponding devID/devEUI for the equivalent HWID

var dncd = {}
dncd["orgname"] = req.query.client
dncd["time"] = [fmdttime, todttime]
dncd["tags"] = {}


var options = {
url: constants.DNC_URL+"gdevmap",
method: 'POST', // Don't forget this line
url: constants.DNC_URL+"tagsk",
method: 'POST',
headers: {'Content-Type': 'application/json' },
// form: {'cname':req.query.client, 'tagval': req.query.device, 'fmdate': fmdttime, 'todate': todttime}
form: {'cname':req.query.client, 'tagval': req.query.device}
//form: {'cname':req.query.cname}
form: {'influxd': {'uname': req.query.client}}
};

request(options, function(error,resp) {
Expand All @@ -97,66 +101,152 @@ exports.readData = (req, res) => {
if(resp.statusCode == 200)
{
var dout = JSON.parse(resp.body)
req.query.device = dout.devices[0]
req.query.dbdata = dout.dbdata
fetchFromInflux(req, res, fmdttime, todttime);
var dnctags = dout.message
var tagvals = req.query.device.split(',')

var dncdict = {}
for(let i=0; i<dnctags.length; i++){
dncd.tags[dnctags[i]] = tagvals[i].trim()
}

// Send request to get device map
var options = {
url: constants.DNC_URL+"gdlist",
method: 'POST',
headers: {'Content-Type': 'application/json' },
form: {'dncd': dncd}
};


request(options, async function(error,resp) {
if(error)
{
res.status(500).send('connect to application failed!');
}
else
{
if(resp.statusCode == 200)
{
var dout = JSON.parse(resp.body)

let sdpromisearr = []

for(let i=0; i<dout.message.devices.length; i++){

let ifdict = {}
ifdict['device'] = dout.message.devices[i]['devid']
ifdict['type'] = dout.message.devices[i]['devtype']
ifdict['sdata'] = req.query.sdata
ifdict['aggfn'] = req.query.aggfn
ifdict['gbt'] = req.query.gbt

let timespan = maptimespan(dncd["time"], [dout.message.devices[i]['idate'],
dout.message.devices[i]['rdate']])
ifdict['fmdate'] = timespan[0]
ifdict['todate'] = timespan[1]
if(dout.message.devices[i]['rdate'] == null){
ifdict['todate'] = dout.message.time[1]
}
ifdict['aggfn'] = req.query.aggfn
ifdict['db'] = dout.message.dsrc[dout.message.devices[i].dsid]

ifdict['math'] = ''
if(req.query.math){
var mathstr = req.query.math
var newstr = mathstr.replace("+", "%2B")
ifdict['math'] = newstr
}

sdpromisearr.push(readsens.readInflux(ifdict))
}

const sdpromises = Promise.allSettled(sdpromisearr)
const dstatuses = await sdpromises

let gresult = []
for(let rstatus of dstatuses){
if(rstatus.status == 'fulfilled'){
gresult.push(rstatus.value)
}
else{
console.log("Data fetch not success: ", rstatus.reason)
}
}

const finalresult = {
Columns: gresult[0].Columns,
Values: []
}

for(let i=0; i<gresult.length; i++){
if(gresult.length > (i+1)){
let d1len = gresult[i].Values.length
let d1date = gresult[i].Values[d1len-1]

let d2date = gresult[i+1].Values[0]

if(d1date[0] === d2date[0]){
if(d1date[1] === null && d2date[1] === null){
gresult[i].Values.pop()
}
else
if(d1date[1] != null && d2date[1] != null){
gresult[i].Values.pop()
}
else
if(d1date[1] === null){
gresult[i].Values.pop()
}
else{
gresult[i+1].Values.shift()
}
}
}
finalresult.Values = finalresult.Values.concat(gresult[i].Values)
}

res.status(200).send({"results": [finalresult]});

}
else
{
res.status(200).send({"message": "Data Read error"});
}
}
});


}
else
{
res.status(500).send(resp.body);
console.log("Tag Key Receive failed")
return res.status(200).send({
message: "Data not available for the sensor"
});
}
}
});

}


async function fetchFromInflux(req, res, fmdate, todate)
{
var influxset = {};
function maptimespan(reqtime, devtime){
let fmdate = reqtime[0]
let todate = reqtime[1]

var datefm = new Date(fmdate);
var dateto = new Date(todate);

influxset.server = req.query.dbdata.url;
influxset.db = req.query.dbdata.dbname;
influxset.measure = req.query.dbdata.mmtname;

influxset.fncode = "";
influxset.user = req.query.dbdata.user
influxset.pass = req.query.dbdata.pwd
let idate = new Date(devtime[0]);
let rdate = devtime[1] == null ? todate : new Date(devtime[1])

influxset.fmdate = datefm;
influxset.todate = dateto;
let influxfmdt = idate, influxtodt = rdate;

influxset.sdata = req.query.sdata
influxset.device = req.query.device
influxset.aggfn = req.query.aggfn
influxset.gbt = req.query.gbt


influxset.math = "";
if(req.query.math)
if(idate.getTime() < fmdate.getTime())
{
var mathstr = req.query.math
var newstr = mathstr.replace("+", "%2B")
influxset.math = newstr
influxfmdt = fmdate;
}

try{
influxdata = await readsens.readInflux(influxset)
if(influxdata != 'error')
{
res.status(200).send({"results": [influxdata]});
}
else
{
res.status(200).send({"message": "Data Read error"});
}

}catch(err){
return res.status(200).send({
message: "Data not available for the sensor"
});
if(rdate.getTime() > todate.getTime())
{
influxtodt = todate;
}
}

return [influxfmdt, influxtodt]

}
Loading

0 comments on commit fefc9db

Please sign in to comment.