// Example For Select Current 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 +".machlake.com/lakes/values/current";
// CASE - Current DATA GET with date format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn
app.get(
'/select_current_date_with_nano_time_date_string', function(req, res) {
var request = require('request');
const QueryParams = new URLSearchParams()
QueryParams.set('tag_name', 'sensor1,sensor2')
QueryParams.set('date_format', 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn')
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":"raw","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor2","data":[{"TIME":"2021-01-07 02:00:00 009:000:000","VALUE":2}]},{"tag_name":"sensor1","data":[{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]}]},"status":"success"}
} else {
console.log(error)
}
})
}
)
// CASE - Current DATA GET with another value form
app.get(
'/select_current_date_with_nano_time_date_string_another_form', function(req, res) {
var request = require('request');
const QueryParams = new URLSearchParams()
QueryParams.set('tag_name', 'sensor1,sensor2')
QueryParams.set('date_format', 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn')
QueryParams.set('value_return_form', 1)
var option = {
url: URL + "?" + QueryParams.toString(),
CLOUD_VENDOR";
const CLOUD_REGION="CLOUD_REGION";
const URL = `https://${CLOUD_VENDOR}.${CLOUD_REGION}.machlake.com/v1/lakes/${LAKE_ID}/values`;
var request = require('request');
const TAG_NAME = "sensor1,sensor2";
const SELECT_TYPE="current";
/* ------------------------------------------------------------------------------------------------- */
// CASE - Current DATA GET with date format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn
request.get({
url: URL,
qs: {
type: SELECT_TYPE,
headerstag_name: { TAG_NAME,
date_format: "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn"
'Content-Type': 'application/json' },
headers: {
'"x-api-key'": API_KEY
}},
function(error, response, body) {
console.log(body);
}, // Return };Format
request.get( option, function(error, response, body) {// {
// "success": true,
if (!error) { // "reason": "success",
res.writeHead(200); // "data":{
res.end(body); // "columns":[
console.log(body) // {"data":{"calc_mode":"raw","columns":[{"name":"TIME","type":5,"length":4096}, {"name":"VALUENAME","type":205,"length":1780}],"samples":[{"tag_name":"sensor2","data":{"TIME":["2021-01-07 02:00:00 009:000:000"],"VALUE":[2]}},{"tag_name":"sensor1","data":{"TIME":["2021-01-06 20:00:06 000:000:000"],"VALUE":[2]}}]},"status":"success"}
,
// {"name":"TIME","type":5,"length":4096},
// {"name":"VALUE","type":20,"length":17}
// ],
// }"rows":[
else { // console.log(error) ["sensor2","2021-01-07 02:00:00 009:000:000",2],
// } }) ["sensor1","2021-01-06 }
)
app.listen(8888, function() {20:00:06 000:000:000",2]
console.log('http://127.0.0.1:8888/select_current_date_with_nano_time_date_string is result for select current data with nano date time string')// ]
console.log('http://127.0.0.1:8888/select_current_date_with_nano_time_date_string_another_form is result for select current data with nano date time string with value return form = 1')
})// }
// }
})
|