not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
5.5 KiB

  1. //* comment *//
  2. Copyright (C) 2015 Walter Bender
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. DEVSERVER: 'https://192.168.2.177:8092/impact/_design/dev_impact_getrows/_view/Get_All_Docs'
  14. DEVSERVER: 'https://192.168.2.177:54692/ProxyHandler.ashx'
  15. //* globals *//
  16. var impactLastFetchTime = 0;
  17. var impactUID = '';
  18. var impactHoursAgo = 0;
  19. var impactData = null;
  20. //* block:fetchdata *//
  21. var fetchBlk = new ProtoBlock('fetch');
  22. fetchBlk.palette = palettes.dict['cloud'];
  23. blocks.protoBlockDict['fetch'] = fetchBlk;
  24. fetchBlk.staticLabels.push(_('fetch'), _('UID'), _('field'), _('hours ago'));
  25. fetchBlk.adjustWidthToLabel();
  26. fetchBlk.threeArgMathBlock();
  27. fetchBlk.dockTypes[1] = 'anyin';
  28. fetchBlk.dockTypes[2] = 'anyin';
  29. fetchBlk.dockTypes[3] = 'numberin';
  30. fetchBlk.defaults.push("UID");
  31. fetchBlk.defaults.push("temp");
  32. fetchBlk.defaults.push(0);
  33. //* arg:fetch *//
  34. var block = logo.blocks.blockList[blk];
  35. var conns = block.connections;
  36. var field = logo.parseArg(logo, turtle, conns[2]);
  37. var uid = logo.parseArg(logo, turtle, conns[1]);
  38. var hoursAgo = logo.parseArg(logo, turtle, conns[3]);
  39. var d = new Date();
  40. var now = d.getTime() / 1000;
  41. var deltaSeconds = now - impactLastFetchTime;
  42. var haveData = false;
  43. if (deltaSeconds > 3600 || uid != impactUID) {
  44. var apiURL = 'https://sensor-station.appspot.com/sensor_station';
  45. console.log('using server data');
  46. try {
  47. var request = new XMLHttpRequest();
  48. request.open('GET', apiURL, false);
  49. request.send(null);
  50. var response = request.responseText;
  51. console.log(response);
  52. } catch (e) {
  53. logo.errorMsg(_('Impact data not available.'), blk);
  54. }
  55. try {
  56. impactData = JSON.parse(response);
  57. haveData = true;
  58. } catch (e) {
  59. logo.errorMsg(_('Cannot parse Impact data.'), blk);
  60. }
  61. } else {
  62. haveData = true;
  63. console.log('using cached data');
  64. }
  65. var output = 0;
  66. if (haveData) {
  67. impactLastFetchTime = now;
  68. impactUID = uid;
  69. var foundUID = false;
  70. var foundTime = false;
  71. if (hoursAgo < 0) {
  72. var minTime = 0;
  73. var maxTime = now;
  74. } else {
  75. var minTime = now - hoursAgo * 3600 - 1800;
  76. var maxTime = now - hoursAgo * 3600 + 1800;
  77. }
  78. try {
  79. for (var i = impactData.length - 1; i > -1; i--) {
  80. if ('uid' in impactData[i] && impactData[i]['uid'] == uid) {
  81. foundUID = true;
  82. if ('tstamp' in impactData[i]) {
  83. var tstamp = impactData[i]['tstamp'] / 1000;
  84. if (tstamp > minTime && tstamp < maxTime) {
  85. foundTime = true;
  86. if (field in impactData[i]) {
  87. output = impactData[i][field];
  88. } else {
  89. logo.errorMsg(_('Impact field not found.'), blk);
  90. }
  91. break;
  92. }
  93. }
  94. } else if ('id' in impactData[i] && impactData[i]['id'] == uid) {
  95. foundUID = true;
  96. if ('tstamp' in impactData[i]) {
  97. var tstamp = impactData[i]['tstamp'] / 1000;
  98. if (tstamp > minTime && tstamp < maxTime) {
  99. foundTime = true;
  100. if (field in impactData[i]) {
  101. output = impactData[i][field];
  102. } else {
  103. logo.errorMsg(_('Impact field not found.'), blk);
  104. }
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. if (!foundUID) {
  111. logo.errorMsg(_('Impact UID not found.'), blk);
  112. } else if (!foundTime) {
  113. logo.errorMsg(_('Impact Time not found.'), blk);
  114. }
  115. } catch (e) {
  116. logo.errorMsg(_('Impact data not available.'), blk);
  117. }
  118. }
  119. block.value = Number(output);
  120. //* palette-fill:cloud *// #ff0066
  121. //* palette-stroke:cloud *// #ef003e
  122. //* palette-highlight:cloud *// #ffb1b3
  123. //* palette-stroke-highlight:cloud *// #000000
  124. //* palette-icon:cloud *//
  125. <?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg xmlns="http://www.w3.org/2000/svg" width="55" height="55"> <path d="m 47.031373,28.122706 c 0.08359,-0.684185 0.126451,-1.382797 0.126451,-2.093434 0,-8.389358 -6.026799,-15.190307 -13.461708,-15.190307 -5.479737,0 -10.194871,3.694469 -12.295784,8.996587 -1.23558,-1.157941 -2.822651,-1.853547 -4.554392,-1.853547 -3.93928,0 -7.1327126,3.603084 -7.1327126,8.047869 0,0.660134 0.070727,1.30163 0.2030723,1.915472 -0.081978,-0.0024 -0.1650298,-0.0036 -0.2475446,-0.0036 -4.9476761,-0.0012 -8.95929097,4.524747 -8.95929097,10.108836 0,5.584087 4.01161487,10.110038 9.00429907,10.210439 L 45.374646,48.160634 c 4.928923,-0.02706 8.91589,-4.542785 8.91589,-10.110039 0,-4.926959 -3.123777,-9.031457 -7.259163,-9.927868 z" style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:1.41892922;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none" /></svg>