27.03.2024, 08:22
Custom JS to fetch trend data from trends.lp:
Both trend ID and name can be specified. Set resolution to 86400 to fetch daily data. Set timestamps to true to fetch both data and timestamps.
trends.lp
Note that there is no input data validation so the script can end with 500 Internal Server Error if the input data is incorrect.
Code:
function getdate(offset) {
var date = new Date();
date.setDate(date.getDate() + offset);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
return { year, month, day };
}
function getdata() {
var trends = [
1,
'Temperature',
'Humidity',
];
var data = JSON.stringify({
trends: trends,
dates: {
start: getdate(0),
end: getdate(1),
},
// resolution: 86400,
// timestamps: true,
});
$.post('/user/trends.lp', data, function(resp) {
console.log(resp);
});
}
Both trend ID and name can be specified. Set resolution to 86400 to fetch daily data. Set timestamps to true to fetch both data and timestamps.
trends.lp
Code:
<?
require('json')
require('trends')
trends.NaN = json.null
ngx.header.content_type = 'application/json'
local req = ngx.req.get_body_data()
req = json.pdecode(req)
local res = {}
local dates = req.dates
local resolution = tonumber(req.resolution)
local timestamps = toboolean(req.timestamps)
for i, idname in ipairs(req.trends) do
local id = tonumber(idname)
if id then
res[ i ] = trends.fetchrangebyid(id, dates, resolution, timestamps)
else
res[ i ] = trends.fetch(idname, dates, resolution, timestamps)
end
end
json.write(res)
Note that there is no input data validation so the script can end with 500 Internal Server Error if the input data is incorrect.