// Example For Select Calculated Data for tag API by using request in node js
// written by yeony kim
// sensor1, sensor2 is applied in lake
// process.env.NODE_TLS_REJECT_UNAUTHORIZED ="0";
const express = require('express');
const app = express();
const API_KEY ="YOUR_API_TOKEN";
const LAKE_ID ="YOUR_LAKE_ID";
const URL CLOUD_VENDOR="https://" + LAKE_ID +".CLOUD_VENDOR";
const CLOUD_REGION="CLOUD_REGION";
const TAG_NAME = "YOUR_TAG";
const URL = `https://${CLOUD_VENDOR}.${CLOUD_REGION}.machlake.com/lakes/${LAKE_ID}/values/calculated"calculated`;
var request = require('request');
// CASE - Calculated DATA GET with date format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn with calc_mode=min
apprequest.get({
'/select_calculated_date_with_nano_time_date_string', function(req, res) {
url: URL,
qs: {
var request = require('request');
const QueryParams = new URLSearchParams()
QueryParams.set('tag_name', 'sensor1,sensor2'): TAG_NAME,
QueryParams.set('calc_mode',: 'min'),
QueryParams.set('date_format',: 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn'),
QueryParams.set('start_time',: '2021-01-06 17:00:00 000:000:000'),
QueryParams.set('end_time',: '2021-01-06 21:00:00 000:000:000')
},
varheaders: option{
= { 'x-api-key': API_KEY
}}, url:
URL + "?" + QueryParams.toStringfunction()error, response, body) {
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY
},
};
request.get( option, function(error, response, body) {
if (!error) {
res.writeHead(200);
res.end(body);
console.log(body) // {"data":{"calc_mode":"MIN","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[// {"data":{"calc_mode":"MIN","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor1","data":[{"TIME":"2021-01-06 17:00:00 000:000:000","VALUE":1},{"TIME":"2021-01-06 18:00:00 000:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:04 000:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:05 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]},{"tag_name":"sensor1sensor2","data":[{"TIME":"2021-01-06 17:00:00 000:000:000","VALUE":1},{"TIME":"2021-01-06 1817:00:0001 000:000:000","VALUE":21.5},{"TIME":"2021-01-06 2017:00:0402 000:000:000","VALUE":2},{"TIME":"2021-01-06 2017:00:0503 000:000:000","VALUE":2.51},{"TIME":"2021-01-06 2017:00:0604 000:000:000","VALUE":2}]1.5},{"tag_name":"sensor2","data":[{"TIME"TIME":"2021-01-06 17:00:0005 000:000:000","VALUE":12},{"TIME":"2021-01-06 1718:00:01 000:000:000","VALUE":1.5},{"TIME":"2021-01-06 17:00:02 000:000:000","VALUE":2},{"TIME":"2021-01-06 1720:00:0304 000:000:000","VALUE":12},{"TIME":"2021-01-06 1720:00:0405 000:000:000","VALUE":12.5},{"TIME":"2021-01-06 1720:00:0506 000:000:000","VALUE":2}]}]},{"TIMEstatus":"2021-01-06 18:00:00 000:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:04 000:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:05 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]}]},"status":"success"}
"success"}
console.log(body)
})
// CASE }- elseCalculated {DATA GET with date format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn with calc_mode=max and interval_value=3
request.get({
console.log(error)
url: URL,
qs: {
} tag_name: TAG_NAME,
}) }
)calc_mode: 'max',
// CASE - Calculated DATA GET with date _format: 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
with calc_mode=max and interval_value=3 app.get( start_time: '2021-01-06 17:00:00 '/select_calculated_date_with_nano_time_date_string_with_max_option', function(req, res) {000:000:000',
end_time: '2021-01-06 21:00:00 000:000:000',
interval_value: 3
var request = require('request');
},
headers: {
const QueryParams = new URLSearchParams() 'x-api-key': API_KEY
}},
QueryParams.set('tag_name', 'sensor1,sensor2')
function(error, response, body) {
QueryParams.set(' // {"data":{"calc_mode', 'max')
QueryParams.set('date_format', 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn')
QueryParams.set('start_time', '2021":"MAX","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor2","data":[{"TIME":"2021-01-06 17:00:00 000:000:000')
QueryParams.set('end_time', '","VALUE":2},{"TIME":"2021-01-06 17:00:03 000:000:000","VALUE":2},{"TIME":"2021-01-06 2118:00:00 000:000:000')
QueryParams.set('interval_value', 3)
var option = {
url: URL + "?" + QueryParams.toString(),
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY
","VALUE":2.5},{"TIME":"2021-01-06 20:00:03 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]},{"tag_name":"sensor1","data":[{"TIME":"2021-01-06 17:00:00 000:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:03 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]}]},"status":"success"}
},console.log(body)
})
// };CASE - Calculated DATA GET with date request.get( option, function(error, response, body) {
format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn with calc_mode=min and interval_type=m and interval_value=90
request.get({
url: URL,
if (!error) { qs: {
tag_name: TAG_NAME,
res.writeHead(200); calc_mode: 'max',
date_format: res.end(body);
'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
start_time: '2021-01-06 17:00:00 000:000:000',
console.log(body) // {"data":{"calc_mode":"MAX","columns":[{"name":"TIME","type":5,"length":4096},{"name":" end_time: '2021-01-06 21:00:00 000:000:000',
interval_type: 'm',
interval_value: 90
},
headers: {
'x-api-key': API_KEY
}},
function(error, response, body) {
// {"data":{"calc_mode":"MAX","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor2","data":[{"TIME":"2021-01-06 1716:0030:00 000:000:000","VALUE":2},{"TIME":"2021-01-06 1718:00:0300 000:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 2019:30:00:03 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]},{"tag_name":"sensor1","data":[{"TIME":"2021-01-06 1716:0030:00 000:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:03 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]}]},"status":"success"}
} else {
console.log(error)
}
})
}
)
// CASE - Calculated DATA GET with date format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn with calc_mode=min and interval_type=m and interval_value=90
app.get(
'/select_calculated_date_with_nano_time_date_string_with_max_interval_option', function(req, res) {
var request = require('request');
const QueryParams = new URLSearchParams()
QueryParams.set('tag_name', 'sensor1,sensor2')
QueryParams.set('calc_mode', 'max')
QueryParams.set('date_format', 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn')
QueryParams.set('start_time', '2021-01-06 17:00:00 000:000:000')
QueryParams.set('end_time', '2021-01-06 21:00:00 000:000:000')
QueryParams.set('interval_type', 'm')
QueryParams.set('interval_value', 90)
var option = {
url: URL + "?" + QueryParams.toString(),
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY
},
};
request.get( option, function(error, response, body) {
if (!error) {
res.writeHead(200);
res.end(body);
console.log(body) // {"data":{"calc_mode":"MAX","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor2","data":[{"TIME":"2021-01-06 16:30:00 000:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 19:30:00 000:000:000","VALUE":2.5}]},{"tag_name":"sensor1","data":[{"TIME":"2021-01-06 16:30:00 000:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 19:30:00 000:000:000","VALUE":2.5}]}]},"status":"success"}
} else {
console.log(error)
}
})
}
)
app.listen(8888, function() {
console.log('http://127.0.0.1:8888/select_current_date_with_nano_time_date_string is result for select calculated data with nano date time string with calc_mode=min')
console.log('http://127.0.0.1:8888/select_calculated_date_with_nano_time_date_string_with_max_option is result for select calculated data with nano date time string with calc_mode=max and interval_value=3')
console.log('http://127.0.0.1:8888/select_calculated_date_with_nano_time_date_string_with_max_interval_option is result for select calculated data with nano date time string with calc_mode=max and interval_type=m interval_value=90')2021-01-06 19:30:00 000:000:000","VALUE":2.5}]}]},"status":"success"}
console.log(body)
}) |