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.

430 lines
13 KiB

  1. //* comment *//
  2. Copyright (C) 2015, 2016 Walter Bender
  3. Copyright (C) 2015 Martin Abente Lahaye
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. //* globals *//
  15. var RODI_BASE_URL = 'http://192.168.4.1:1234';
  16. var rodiSensorsCache = null;
  17. var rodiSensorsValidFor = 100;
  18. var rodiSensorsCacheExpires = 0;
  19. var rodiLightSensorCache = null;
  20. var rodiLightSensorValidFor = 100;
  21. var rodiLightSensorCacheExpires = 0;
  22. var rodiMoveStatus = _('stop');
  23. var rodiDistanceCache = null;
  24. var rodiDistanceValidFor = 100;
  25. var rodiDistanceCacheExpires = 0;
  26. var doRodiGetDistance = function (logo, blk) {
  27. if (logo.inStatusMatrix) {
  28. logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
  29. } else {
  30. if (Date.now() > rodiDistanceCacheExpires) {
  31. var apiUrl = RODI_BASE_URL + '/5/';
  32. var distance = doSyncCall(logo, blk, apiUrl);
  33. if (distance != null) {
  34. rodiDistanceCacheExpires = Date.now() + rodiDistanceValidFor;
  35. rodiDistanceCache = distance;
  36. return distance;
  37. } else {
  38. return 100;
  39. }
  40. } else {
  41. return rodiDistanceCache;
  42. }
  43. }
  44. };
  45. var doRodiGetMoveStatus = function (logo, blk) {
  46. if (logo.inStatusMatrix) {
  47. logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
  48. } else {
  49. return rodiMoveStatus;
  50. }
  51. };
  52. var doRodiMove = function (logo, blk, left, right) {
  53. var apiUrl = RODI_BASE_URL + '/3/' + left + '/' + right + '/';
  54. if (left === right) {
  55. if (left < 0) {
  56. rodiMoveStatus = _('back');
  57. } else if (left > 0) {
  58. rodiMoveStatus = _('forward');
  59. } else {
  60. rodiMoveStatus = _('stop');
  61. }
  62. } else {
  63. if (left > right) {
  64. rodiMoveStatus = _('right');
  65. } else {
  66. rodiMoveStatus = _('left');
  67. }
  68. }
  69. doAsyncCall(logo, blk, apiUrl)
  70. };
  71. var doRodiGetSensor = function (logo, blk, index) {
  72. if (logo.inStatusMatrix) {
  73. logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
  74. } else {
  75. if (Date.now() > rodiSensorsCacheExpires) {
  76. var apiUrl = RODI_BASE_URL + '/2/';
  77. var sensors = doSyncCall(logo, blk, apiUrl);
  78. if (sensors != null) {
  79. rodiSensorsCacheExpires = Date.now() + rodiSensorsValidFor;
  80. rodiSensorsCache = sensors;
  81. return sensors[index];
  82. } else {
  83. return -1;
  84. }
  85. } else {
  86. return rodiSensorsCache[index];
  87. }
  88. }
  89. };
  90. var doRodiGetLightSensor = function (logo, blk) {
  91. if (logo.inStatusMatrix) {
  92. logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
  93. } else {
  94. if (Date.now() > rodiLightSensorCacheExpires) {
  95. var apiUrl = RODI_BASE_URL + '/7/';
  96. var sensor = doSyncCall(logo, blk, apiUrl);
  97. if (sensor != null) {
  98. rodiLightSensorCacheExpires = Date.now() + rodiLightSensorValidFor;
  99. rodiLightSensorCache = sensor;
  100. return sensor;
  101. } else {
  102. return -1;
  103. }
  104. } else {
  105. return rodiLightSensorCache;
  106. }
  107. }
  108. };
  109. var doAsyncCall = function (logo, blk, url){
  110. console.log(url);
  111. try {
  112. var request = new XMLHttpRequest();
  113. request.open('GET', url, true);
  114. request.send(null);
  115. } catch (e) {
  116. if (logo != null) {
  117. logo.errorMsg(e, blk);
  118. }
  119. console.log(e);
  120. }
  121. };
  122. var doSyncCall = function (logo, blk, url) {
  123. console.log(url);
  124. var request = new XMLHttpRequest();
  125. try {
  126. request.open('GET', url, false);
  127. request.send(null);
  128. return JSON.parse(request.responseText);
  129. } catch (e) {
  130. if (logo != null) {
  131. logo.errorMsg(e, blk);
  132. }
  133. console.log(e);
  134. return null;
  135. }
  136. };
  137. function doRodiStop(){
  138. doRodiMove(null, null, 0, 0);
  139. };
  140. blocks.logo.stage.addEventListener('__STOP__', doRodiStop, false);
  141. //* block:rodiblink *//
  142. var rodiBlink = new ProtoBlock('rodiblink');
  143. rodiBlink.palette = palettes.dict['rodi'];
  144. blocks.protoBlockDict['rodiblink'] = rodiBlink;
  145. rodiBlink.staticLabels.push(_('blink'));
  146. rodiBlink.adjustWidthToLabel();
  147. rodiBlink.oneArgBlock();
  148. rodiBlink.defaults.push(1000);
  149. //* flow:rodiblink *//
  150. var block = logo.blocks.blockList[blk];
  151. var conns = block.connections;
  152. var rate = logo.parseArg(logo, turtle, conns[1]);
  153. var apiUrl = RODI_BASE_URL + '/1/' + rate + '/';
  154. doAsyncCall(logo, blk, apiUrl);
  155. //* block:rodising *//
  156. var rodiSing = new ProtoBlock('rodising');
  157. rodiSing.palette = palettes.dict['rodi'];
  158. blocks.protoBlockDict['rodising'] = rodiSing;
  159. rodiSing.staticLabels.push(_('play'));
  160. rodiSing.staticLabels.push(_('note'));
  161. rodiSing.staticLabels.push(_('duration'));
  162. rodiSing.adjustWidthToLabel();
  163. rodiSing.twoArgBlock();
  164. rodiSing.dockTypes[1] = 'numberin';
  165. rodiSing.dockTypes[2] = 'numberin';
  166. rodiSing.defaults.push(31);
  167. rodiSing.defaults.push(250);
  168. //* flow:rodising *//
  169. var block = logo.blocks.blockList[blk];
  170. var conns = block.connections;
  171. var note = logo.parseArg(logo, turtle, conns[1]);
  172. var duration = logo.parseArg(logo, turtle, conns[2]);
  173. var apiUrl = RODI_BASE_URL + '/4/' + note + '/' + duration + '/';
  174. doAsyncCall(logo, blk, apiUrl);
  175. //* block:rodipixel *//
  176. var rodiPixel = new ProtoBlock('rodipixel');
  177. rodiPixel.palette = palettes.dict['rodi'];
  178. blocks.protoBlockDict['rodipixel'] = rodiPixel;
  179. rodiPixel.staticLabels.push(_('pixel color'));
  180. rodiPixel.staticLabels.push(_('red'));
  181. rodiPixel.staticLabels.push(_('green'));
  182. rodiPixel.staticLabels.push(_('blue'));
  183. rodiPixel.adjustWidthToLabel();
  184. rodiPixel.threeArgBlock();
  185. rodiPixel.dockTypes[1] = 'numberin';
  186. rodiPixel.dockTypes[2] = 'numberin';
  187. rodiPixel.dockTypes[3] = 'numberin';
  188. rodiPixel.defaults.push(0);
  189. rodiPixel.defaults.push(0);
  190. rodiPixel.defaults.push(0);
  191. //* flow:rodipixel *//
  192. var block = logo.blocks.blockList[blk];
  193. var conns = block.connections;
  194. var red = logo.parseArg(logo, turtle, conns[1]);
  195. var green = logo.parseArg(logo, turtle, conns[2]);
  196. var blue = logo.parseArg(logo, turtle, conns[3]);
  197. var apiUrl = RODI_BASE_URL + '/6/' + red + '/' + green + '/' + blue + '/';
  198. doAsyncCall(logo, blk, apiUrl);
  199. //* block:rodiled *//
  200. var rodiLed = new ProtoBlock('rodiled');
  201. rodiLed.palette = palettes.dict['rodi'];
  202. blocks.protoBlockDict['rodiled'] = rodiLed;
  203. rodiLed.staticLabels.push(_('led'));
  204. rodiLed.adjustWidthToLabel();
  205. rodiLed.oneArgBlock();
  206. rodiLed.defaults.push(1);
  207. //* flow:rodiled *//
  208. var block = logo.blocks.blockList[blk];
  209. var conns = block.connections;
  210. var state = logo.parseArg(logo, turtle, conns[1]);
  211. var apiUrl = RODI_BASE_URL + '/8/' + state + '/';
  212. doAsyncCall(logo, blk, apiUrl);
  213. //* block:rodisenselight *//
  214. var rodiSenseLight = new ProtoBlock('rodisenselight');
  215. rodiSenseLight.palette = palettes.dict['rodi'];
  216. blocks.protoBlockDict['rodisenselight'] = rodiSenseLight;
  217. rodiSenseLight.staticLabels.push(_('light intensity'));
  218. rodiSenseLight.adjustWidthToLabel();
  219. rodiSenseLight.parameterBlock();
  220. //* arg:rodisenselight *//
  221. var block = logo.blocks.blockList[blk];
  222. block.value = doRodiGetLightSensor(logo, blk);
  223. //* block:rodisenseleft *//
  224. var rodiSenseLeft = new ProtoBlock('rodisenseleft');
  225. rodiSenseLeft.palette = palettes.dict['rodi'];
  226. blocks.protoBlockDict['rodisenseleft'] = rodiSenseLeft;
  227. rodiSenseLeft.staticLabels.push(_('infrared light (left)'));
  228. rodiSenseLeft.adjustWidthToLabel();
  229. rodiSenseLeft.parameterBlock();
  230. //* arg:rodisenseleft *//
  231. var block = logo.blocks.blockList[blk];
  232. block.value = doRodiGetSensor(logo, blk, 0);
  233. //* block:rodisenseright *//
  234. var rodiSenseRight = new ProtoBlock('rodisenseright');
  235. rodiSenseRight.palette = palettes.dict['rodi'];
  236. blocks.protoBlockDict['rodisenseright'] = rodiSenseRight;
  237. rodiSenseRight.staticLabels.push(_('infrared light (right)'));
  238. rodiSenseRight.adjustWidthToLabel();
  239. rodiSenseRight.parameterBlock();
  240. //* arg:rodisenseright *//
  241. var block = logo.blocks.blockList[blk];
  242. block.value = doRodiGetSensor(logo, blk, 1);
  243. //* block:rodisee *//
  244. var rodiSee = new ProtoBlock('rodisee');
  245. rodiSee.palette = palettes.dict['rodi'];
  246. blocks.protoBlockDict['rodisee'] = rodiSee;
  247. rodiSee.staticLabels.push(_('distance'));
  248. rodiSee.adjustWidthToLabel();
  249. rodiSee.parameterBlock();
  250. //* arg:rodisee *//
  251. var block = logo.blocks.blockList[blk];
  252. block.value = doRodiGetDistance(logo, blk);
  253. //* block:rodimovestatus *//
  254. var rodiMoveStatus = new ProtoBlock('rodimovestatus');
  255. rodiMoveStatus.palette = palettes.dict['rodi'];
  256. blocks.protoBlockDict['rodimovestatus'] = rodiMoveStatus;
  257. rodiMoveStatus.staticLabels.push(_('status'));
  258. rodiMoveStatus.adjustWidthToLabel();
  259. rodiMoveStatus.parameterBlock();
  260. //* arg:rodimovestatus *//
  261. var block = logo.blocks.blockList[blk];
  262. block.value = doRodiGetMoveStatus(logo, blk);
  263. //* block:rodimove *//
  264. var rodiMove = new ProtoBlock('rodimove');
  265. rodiMove.palette = palettes.dict['rodi'];
  266. blocks.protoBlockDict['rodimove'] = rodiMove;
  267. rodiMove.staticLabels.push(_('move'));
  268. rodiMove.staticLabels.push(_('left'));
  269. rodiMove.staticLabels.push(_('right'));
  270. rodiMove.adjustWidthToLabel();
  271. rodiMove.twoArgBlock();
  272. rodiMove.dockTypes[1] = 'numberin';
  273. rodiMove.dockTypes[2] = 'numberin';
  274. rodiMove.defaults.push(100);
  275. rodiMove.defaults.push(100);
  276. //* flow:rodimove *//
  277. var block = logo.blocks.blockList[blk];
  278. var conns = block.connections;
  279. var left = logo.parseArg(logo, turtle, conns[1]);
  280. var right = logo.parseArg(logo, turtle, conns[2]);
  281. doRodiMove(logo, blk, left, right);
  282. //* block:rodileft *//
  283. var rodiLeft = new ProtoBlock('rodileft');
  284. rodiLeft.palette = palettes.dict['rodi'];
  285. blocks.protoBlockDict['rodileft'] = rodiLeft;
  286. rodiLeft.staticLabels.push(_('left'));
  287. rodiLeft.adjustWidthToLabel();
  288. rodiLeft.zeroArgBlock();
  289. //* flow:rodileft *//
  290. doRodiMove(logo, blk, -100, 100);
  291. //* block:rodiright *//
  292. var rodiRight = new ProtoBlock('rodiright');
  293. rodiRight.palette = palettes.dict['rodi'];
  294. blocks.protoBlockDict['rodiright'] = rodiRight;
  295. rodiRight.staticLabels.push(_('right'));
  296. rodiRight.adjustWidthToLabel();
  297. rodiRight.zeroArgBlock();
  298. //* flow:rodiright *//
  299. doRodiMove(logo, blk, 100, -100);
  300. //* block:rodiback *//
  301. var rodiBack = new ProtoBlock('rodiback');
  302. rodiBack.palette = palettes.dict['rodi'];
  303. blocks.protoBlockDict['rodiback'] = rodiBack;
  304. rodiBack.staticLabels.push(_('back'));
  305. rodiBack.adjustWidthToLabel();
  306. rodiBack.zeroArgBlock();
  307. //* flow:rodiback *//
  308. doRodiMove(logo, blk, -100, -100);
  309. //* block:rodiforward *//
  310. var rodiForward = new ProtoBlock('rodiforward');
  311. rodiForward.palette = palettes.dict['rodi'];
  312. blocks.protoBlockDict['rodiforward'] = rodiForward;
  313. rodiForward.staticLabels.push(_('forward'));
  314. rodiForward.adjustWidthToLabel();
  315. rodiForward.zeroArgBlock();
  316. //* flow:rodiforward *//
  317. doRodiMove(logo, blk, 100, 100);
  318. //* block:rodistop *//
  319. var rodiStop = new ProtoBlock('rodistop');
  320. rodiStop.palette = palettes.dict['rodi'];
  321. blocks.protoBlockDict['rodistop'] = rodiStop;
  322. rodiStop.staticLabels.push(_('stop'));
  323. rodiStop.adjustWidthToLabel();
  324. rodiStop.zeroArgBlock();
  325. //* flow:rodistop *//
  326. doRodiMove(logo, blk, 0, 0);
  327. //* palette-icon:rodi *//
  328. <?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" id="svg2" version="1.1" height="55" width="55"> <path d="M 35.516203,8.5290201 C 22.94617,8.4975021 20.536671,8.9380889 15.981939,12.108873 8.040645,17.63697 5.5333178,28.725893 10.310252,37.186933 c 5.057023,8.956964 16.647266,12.342556 25.705513,7.50901 2.79002,-1.488868 6.53595,-5.248313 8.015725,-8.041727 1.480563,-2.794869 2.377437,-4.759722 2.678979,-10.981156 l 0,-17.1440399 -11.194266,0 z M 26.938244,13.920134 A 13.657546,13.657546 0 0 1 40.596015,27.577846 13.657546,13.657546 0 0 1 26.938244,41.235617 13.657546,13.657546 0 0 1 13.280472,27.577846 13.657546,13.657546 0 0 1 26.938244,13.920134 Z M 26.59609,22.93256 a 5.1346998,5.1346998 0 0 0 -5.13424,5.134241 5.1346998,5.1346998 0 0 0 5.13424,5.135393 5.1346998,5.1346998 0 0 0 5.134242,-5.135393 5.1346998,5.1346998 0 0 0 -5.134242,-5.134241 z" style="fill:#009dd8;fill-opacity:1" /> </svg>
  329. //* palette-fill:rodi *// #6a8df6
  330. //* palette-stroke:rodi *// #5a7de6
  331. //* palette-highlight:rodi *// #ced8f8
  332. //* palette-stroke-highlight:rodi *// #000000