-
-
Save nielsvanderbeke/3716e00578ed7d0fa3e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Access Flukso API server returning a JSON | |
* | |
*/ | |
function getJSON(sensor, token, query) { | |
var sheetname = "test"; | |
//var aUrl = "http://api.flukso.net/sensor/c1411c6b4f9910bbbab09f145f8533b9?version=1.0&token=d8a8ab8893ea73f768b66b45234b5c3a&interval=month&unit=watt"; | |
var api="1.0"; | |
var aUrl = "http://api.flukso.net/sensor/"+sensor+"?version="+api+"&token="+token+query; | |
var headers = { | |
"Accept": "application/json", | |
}; | |
var options = { | |
"method": "get", | |
"headers": headers, | |
}; | |
var response = UrlFetchApp.fetch(aUrl); // get feed | |
return JSON.parse(response.getContentText()); | |
} | |
/** | |
* Get information from Flukso, day resolution | |
*/ | |
function getKWhMonth(sensor,token, start, end) { | |
// Prepare query for API server | |
var days = end - start; | |
start = convertDate(start); | |
end = convertDate(end); | |
var query = "&start=" + start +"&end=" + end +"&resolution=day&unit=watt"; | |
var data=getJSON(sensor, token, query); | |
var sum = 0; | |
for (var i = 0; i < data.length; i++) { | |
// data[x] = [ Unix date, Watt] | |
// so, use the second parameter [1] | |
if (typeof data[i][1] == "number") | |
sum = sum + data[i][1]; | |
} | |
// Average KWh | |
return ((sum *24)/1000); | |
} | |
/** | |
* Get information from Flukso, hour resolution | |
*/ | |
function getKWhDay(sensor,token, start, end) { | |
// Prepare query for API server | |
//var days = end - start; | |
start = convertDate(start); | |
end = convertDate(end); | |
var query = "&start=" + start +"&end=" + end +"&resolution=hour&unit=watt"; | |
var data=getJSON(sensor, token, query); | |
var sum = 0; | |
for (var i = 0; i < data.length; i++) { | |
// data[x] = [ Unix date, Watt] | |
// so, use the second parameter [1] | |
if (typeof data[i][1] == "number") | |
sum = sum + data[i][1]; | |
} | |
// Average KWh | |
return (sum/1000); | |
} | |
/** | |
* Converts a given date number to Unix timestamp | |
* (seconds from Jan-1-1970) | |
* Accounts for Timezone Offset | |
*/ | |
function convertDate(dateNumber) { | |
var ret = Date.parse(dateNumber); | |
var dat = new Date(ret); | |
var offset = dat.getTimezoneOffset() * 60; // seconds | |
// return seconds instead of milliseconds | |
return (ret /1000) - offset; | |
} | |
/** | |
* Calculate coast of consumption based on | |
* | |
* 1) KWh coast and company consuption reading | |
* | |
* or | |
* | |
* 2) Previous KWh coast and flukso actual reading | |
*/ | |
function custo(companyRead,fluksoRead,custoActual,custoPrevious) { | |
var ret = 0; | |
if (((companyRead != "") && (typeof companyRead == "number")) && | |
((custoActual != "") && (typeof custoActual == "number"))) { | |
ret = companyRead * custoActual; | |
} else { | |
if (((fluksoRead != "") && (typeof fluksoRead == "number")) && | |
((custoActual != "") && (typeof custoActual == "number"))) { | |
ret = fluksoRead * custoActual; | |
} else { | |
if (((fluksoRead != "") && (typeof fluksoRead == "number")) )//&& | |
// ((custoPrevious != "") && (typeof custoPrevious == "number"))) | |
ret = fluksoRead * custoPrevious; | |
} | |
} | |
return (ret>0) ? ret : ""; | |
} | |
/** | |
* Adds a custom menu to the active spreadsheet, containing a single menu item | |
* for invoking the readRows() function specified above. | |
* The onOpen() function, when defined, is automatically invoked whenever the | |
* spreadsheet is opened. | |
* For more information on using the Spreadsheet API, see | |
* https://developers.google.com/apps-script/service_spreadsheet | |
*/ | |
/*function onOpen() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var entries = [{ | |
name : "getJson", | |
functionName : "getJson" | |
}]; | |
sheet.addMenu("Script Center Menu", entries); | |
};*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment