//* comment *// Copyright (C) 2015 Walter Bender This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . DEVSERVER: 'https://192.168.2.177:8092/impact/_design/dev_impact_getrows/_view/Get_All_Docs' DEVSERVER: 'https://192.168.2.177:54692/ProxyHandler.ashx' //* globals *// var impactLastFetchTime = 0; var impactUID = ''; var impactHoursAgo = 0; var impactData = null; //* block:fetchdata *// var fetchBlk = new ProtoBlock('fetch'); fetchBlk.palette = palettes.dict['cloud']; blocks.protoBlockDict['fetch'] = fetchBlk; fetchBlk.staticLabels.push(_('fetch'), _('UID'), _('field'), _('hours ago')); fetchBlk.adjustWidthToLabel(); fetchBlk.threeArgMathBlock(); fetchBlk.dockTypes[1] = 'anyin'; fetchBlk.dockTypes[2] = 'anyin'; fetchBlk.dockTypes[3] = 'numberin'; fetchBlk.defaults.push("UID"); fetchBlk.defaults.push("temp"); fetchBlk.defaults.push(0); //* arg:fetch *// var block = logo.blocks.blockList[blk]; var conns = block.connections; var field = logo.parseArg(logo, turtle, conns[2]); var uid = logo.parseArg(logo, turtle, conns[1]); var hoursAgo = logo.parseArg(logo, turtle, conns[3]); var d = new Date(); var now = d.getTime() / 1000; var deltaSeconds = now - impactLastFetchTime; var haveData = false; if (deltaSeconds > 3600 || uid != impactUID) { var apiURL = 'https://sensor-station.appspot.com/sensor_station'; console.log('using server data'); try { var request = new XMLHttpRequest(); request.open('GET', apiURL, false); request.send(null); var response = request.responseText; console.log(response); } catch (e) { logo.errorMsg(_('Impact data not available.'), blk); } try { impactData = JSON.parse(response); haveData = true; } catch (e) { logo.errorMsg(_('Cannot parse Impact data.'), blk); } } else { haveData = true; console.log('using cached data'); } var output = 0; if (haveData) { impactLastFetchTime = now; impactUID = uid; var foundUID = false; var foundTime = false; if (hoursAgo < 0) { var minTime = 0; var maxTime = now; } else { var minTime = now - hoursAgo * 3600 - 1800; var maxTime = now - hoursAgo * 3600 + 1800; } try { for (var i = impactData.length - 1; i > -1; i--) { if ('uid' in impactData[i] && impactData[i]['uid'] == uid) { foundUID = true; if ('tstamp' in impactData[i]) { var tstamp = impactData[i]['tstamp'] / 1000; if (tstamp > minTime && tstamp < maxTime) { foundTime = true; if (field in impactData[i]) { output = impactData[i][field]; } else { logo.errorMsg(_('Impact field not found.'), blk); } break; } } } else if ('id' in impactData[i] && impactData[i]['id'] == uid) { foundUID = true; if ('tstamp' in impactData[i]) { var tstamp = impactData[i]['tstamp'] / 1000; if (tstamp > minTime && tstamp < maxTime) { foundTime = true; if (field in impactData[i]) { output = impactData[i][field]; } else { logo.errorMsg(_('Impact field not found.'), blk); } break; } } } } if (!foundUID) { logo.errorMsg(_('Impact UID not found.'), blk); } else if (!foundTime) { logo.errorMsg(_('Impact Time not found.'), blk); } } catch (e) { logo.errorMsg(_('Impact data not available.'), blk); } } block.value = Number(output); //* palette-fill:cloud *// #ff0066 //* palette-stroke:cloud *// #ef003e //* palette-highlight:cloud *// #ffb1b3 //* palette-stroke-highlight:cloud *// #000000 //* palette-icon:cloud *//