wifiserver.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. 'use strict';
  2. goog.provide('Blockly.Blocks.wifiserver');
  3. goog.require('Blockly.Blocks');
  4. goog.require('Blockly.Types');
  5. Blockly.Blocks.wifiserver.HUE = "#293278";
  6. /**wifi server
  7. *setup for server,set port
  8. */
  9. Blockly.Blocks['wifiServer_setup'] = {
  10. init: function() {
  11. this.appendDummyInput()
  12. .appendField(new Blockly.FieldImage(
  13. // "http://cocorobo.cn/cocoblockly/blockly/media/wifi_server.png", 40, 40, "15"))
  14. "./../blockly/media/wifi_server.png", 40, 40, "15"))
  15. this.appendDummyInput()
  16. .appendField(Blockly.Msg.WIFISERVER_SETUP)
  17. this.setColour(Blockly.Blocks.wifiserver.HUE);
  18. this.setTooltip("");
  19. this.setHelpUrl("");
  20. },
  21. };
  22. /**wifi server
  23. *set server Root webpage
  24. *@param {blocks} statement: for webpage block
  25. */
  26. Blockly.Blocks['wifiServer_setRootPage'] = {
  27. init: function() {
  28. this.appendDummyInput()
  29. .appendField(Blockly.Msg.WIFISERVER_SETROUTER)
  30. this.appendDummyInput()
  31. .appendField(Blockly.Msg.WIFISERVER_SETROOTPAGE)
  32. this.appendStatementInput("STATEMENT0")
  33. this.setColour(Blockly.Blocks.wifiserver.HUE);
  34. this.setTooltip("");
  35. this.setHelpUrl("");
  36. },
  37. };
  38. /**wifi server
  39. *set server url with response when HTTP requests
  40. *@param {String} url: String for Router path
  41. *@param {blocks} statement: motion for Router path
  42. */
  43. Blockly.Blocks['wifiServer_setRouter'] = {
  44. init: function() {
  45. this.appendDummyInput()
  46. .appendField(Blockly.Msg.WIFISERVER_SETROUTER)
  47. this.appendDummyInput("ADD0")
  48. .appendField(Blockly.Msg.WIFISERVER_SETROUTER_SET)
  49. .appendField(new Blockly.FieldTextInput("/"), "URL0")
  50. this.appendStatementInput("STATEMENT0")
  51. .appendField(Blockly.Msg.WIFISERVER_SETROUTER_WITH)
  52. this.itemCount_ = 1;
  53. this.setMutator(new Blockly.Mutator(['ServerRouter_create_with_item']));
  54. this.setColour(Blockly.Blocks.wifiserver.HUE);
  55. this.setPreviousStatement(true);
  56. this.setNextStatement(true);
  57. this.setTooltip("");
  58. this.setHelpUrl("");
  59. },
  60. mutationToDom: function() {
  61. var container = document.createElement('mutation');
  62. container.setAttribute('items', this.itemCount_);
  63. return container;
  64. },
  65. /**
  66. * Parse XML to restore the inputs.
  67. * @param {!Element} xmlElement XML storage element.
  68. * @this Blockly.Block
  69. */
  70. domToMutation: function(xmlElement) {
  71. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  72. this.updateShape_();
  73. },
  74. /**
  75. * Populate the mutator's dialog with this block's components.
  76. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  77. * @return {!Blockly.Block} Root block in mutator.
  78. * @this Blockly.Block
  79. */
  80. decompose: function(workspace) {
  81. var containerBlock = workspace.newBlock('ServerRouter_create_with_container');
  82. containerBlock.initSvg();
  83. var connection = containerBlock.getInput('STACK').connection;;
  84. for (var i = 0; i < this.itemCount_; i++) {
  85. var itemBlock = workspace.newBlock('ServerRouter_create_with_item');
  86. itemBlock.initSvg();
  87. connection.connect(itemBlock.previousConnection);
  88. connection = itemBlock.nextConnection;
  89. }
  90. return containerBlock;
  91. },
  92. /**
  93. * Reconfigure this block based on the mutator dialog's components.
  94. * @param {!Blockly.Block} containerBlock Root block in mutator.
  95. * @this Blockly.Block
  96. */
  97. compose: function(containerBlock) {
  98. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  99. // var itemBlock = containerBlock.nextConnection.targetBlock();
  100. // Count number of inputs.
  101. var connections = [];
  102. while (itemBlock) {
  103. connections.push(itemBlock.valueConnection_);
  104. itemBlock = itemBlock.nextConnection &&
  105. itemBlock.nextConnection.targetBlock();
  106. }
  107. // Disconnect any children that don't belong.
  108. // for (var i = 0; i < this.itemCount_; i++) {
  109. // var connection = this.getInput('ADD' + i).connection.targetConnection;
  110. // if (connection && connections.indexOf(connection) == -1) {
  111. // connection.disconnect();
  112. // }
  113. // }
  114. this.itemCount_ = connections.length;
  115. this.updateShape_();
  116. // Reconnect any child blocks.
  117. for (var i = 0; i < this.itemCount_; i++) {
  118. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i)
  119. }
  120. },
  121. /**
  122. * Store pointers to any connected child blocks.
  123. * @param {!Blockly.Block} containerBlock Root block in mutator.
  124. * @this Blockly.Block
  125. */
  126. saveConnections: function(containerBlock) {
  127. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  128. var i = 0;
  129. while (itemBlock) {
  130. var input = this.getInput('ADD' + i);
  131. i++;
  132. itemBlock = itemBlock.nextConnection &&
  133. itemBlock.nextConnection.targetBlock();
  134. }
  135. },
  136. /**
  137. * Modify this block to have the correct number of inputs.
  138. * @private
  139. * @this Blockly.Block
  140. */
  141. updateShape_: function() {
  142. // Add new inputs.
  143. for (var i = 0; i < this.itemCount_; i++) {
  144. if (!this.getInput('ADD' + i)) {
  145. this.appendDummyInput("ADD" + i)
  146. .appendField(Blockly.Msg.WIFISERVER_SETROUTER_SET)
  147. .appendField(new Blockly.FieldTextInput("/"), "URL" + i)
  148. this.appendStatementInput("STATEMENT" + i)
  149. .appendField(Blockly.Msg.WIFISERVER_SETROUTER_WITH)
  150. }
  151. }
  152. // Remove deleted inputs.
  153. while (this.getInput('ADD' + i)) {
  154. this.removeInput('ADD' + i);
  155. this.removeInput('STATEMENT' + i);
  156. i++;
  157. }
  158. }
  159. };
  160. Blockly.Blocks['ServerRouter_create_with_container'] = {
  161. /**
  162. * Mutator block for list container.
  163. * @this Blockly.Block
  164. */
  165. init: function() {
  166. this.appendDummyInput()
  167. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  168. this.appendStatementInput('STACK');
  169. this.setColour(Blockly.Blocks.wifiserver.HUE);
  170. this.setTooltip('');
  171. this.contextMenu = false;
  172. }
  173. };
  174. Blockly.Blocks['ServerRouter_create_with_item'] = {
  175. /**
  176. * Mutator bolck for adding items.
  177. * @this Blockly.Block
  178. */
  179. init: function() {
  180. this.setColour(Blockly.Blocks.wifiserver.HUE);
  181. this.appendDummyInput()
  182. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  183. this.setPreviousStatement(true);
  184. this.setNextStatement(true);
  185. this.setTooltip('');
  186. this.contextMenu = false;
  187. }
  188. };
  189. /**WiFi Server
  190. *send JSON data from Server
  191. *@param {variable} txdata the data name
  192. *@param {String/int} data a collection of data as JSON
  193. */
  194. Blockly.Blocks['wifiServer_Send'] = {
  195. init: function() {
  196. this.appendDummyInput()
  197. .appendField(Blockly.Msg.WIFISERVER_SEND)
  198. this.appendDummyInput()
  199. .appendField(Blockly.Msg.WIFISERVER_LIST)
  200. .appendField(new Blockly.FieldTextInput("Data_send"), "VAR");
  201. this.appendValueInput("ADD0")
  202. .appendField("Data0")
  203. this.itemCount_ = 1;
  204. this.updateShape_();
  205. this.setMutator(new Blockly.Mutator(['ServerSend_create_with_item']));
  206. this.setColour(Blockly.Blocks.wifiserver.HUE);
  207. this.setPreviousStatement(true);
  208. this.setNextStatement(true);
  209. // this.setInputsInline(true);
  210. this.setTooltip("");
  211. this.setHelpUrl("");
  212. },
  213. mutationToDom: function() {
  214. var container = document.createElement('mutation');
  215. container.setAttribute('items', this.itemCount_);
  216. return container;
  217. },
  218. /**
  219. * Parse XML to restore the inputs.
  220. * @param {!Element} xmlElement XML storage element.
  221. * @this Blockly.Block
  222. */
  223. domToMutation: function(xmlElement) {
  224. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  225. this.updateShape_();
  226. },
  227. /**
  228. * Populate the mutator's dialog with this block's components.
  229. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  230. * @return {!Blockly.Block} Root block in mutator.
  231. * @this Blockly.Block
  232. */
  233. decompose: function(workspace) {
  234. var containerBlock = workspace.newBlock('ServerSend_create_with_container');
  235. containerBlock.initSvg();
  236. var connection = containerBlock.getInput('STACK').connection;
  237. for (var i = 0; i < this.itemCount_; i++) {
  238. var itemBlock = workspace.newBlock('ServerSend_create_with_item');
  239. itemBlock.initSvg();
  240. connection.connect(itemBlock.previousConnection);
  241. connection = itemBlock.nextConnection;
  242. }
  243. return containerBlock;
  244. },
  245. /**
  246. * Reconfigure this block based on the mutator dialog's components.
  247. * @param {!Blockly.Block} containerBlock Root block in mutator.
  248. * @this Blockly.Block
  249. */
  250. compose: function(containerBlock) {
  251. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  252. // Count number of inputs.
  253. var connections = [];
  254. while (itemBlock) {
  255. connections.push(itemBlock.valueConnection_);
  256. itemBlock = itemBlock.nextConnection &&
  257. itemBlock.nextConnection.targetBlock();
  258. }
  259. // Disconnect any children that don't belong.
  260. for (var i = 0; i < this.itemCount_; i++) {
  261. var connection = this.getInput('ADD' + i).connection.targetConnection;
  262. if (connection && connections.indexOf(connection) == -1) {
  263. connection.disconnect();
  264. }
  265. }
  266. this.itemCount_ = connections.length;
  267. this.updateShape_();
  268. // Reconnect any child blocks.
  269. for (var i = 0; i < this.itemCount_; i++) {
  270. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  271. }
  272. },
  273. /**
  274. * Store pointers to any connected child blocks.
  275. * @param {!Blockly.Block} containerBlock Root block in mutator.
  276. * @this Blockly.Block
  277. */
  278. saveConnections: function(containerBlock) {
  279. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  280. var i = 0;
  281. while (itemBlock) {
  282. var input = this.getInput('ADD' + i);
  283. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  284. i++;
  285. itemBlock = itemBlock.nextConnection &&
  286. itemBlock.nextConnection.targetBlock();
  287. }
  288. },
  289. /**
  290. * Modify this block to have the correct number of inputs.
  291. * @private
  292. * @this Blockly.Block
  293. */
  294. updateShape_: function() {
  295. // Add new inputs.
  296. for (var i = 0; i < this.itemCount_; i++) {
  297. if (!this.getInput('ADD' + i)) {
  298. var input = this.appendValueInput('ADD' + i);
  299. input.appendField("Data" + i);
  300. }
  301. }
  302. // Remove deleted inputs.
  303. while (this.getInput('ADD' + i)) {
  304. this.removeInput('ADD' + i);
  305. i++;
  306. }
  307. }
  308. };
  309. Blockly.Blocks['ServerSend_create_with_container'] = {
  310. /**
  311. * Mutator block for list container.
  312. * @this Blockly.Block
  313. */
  314. init: function() {
  315. this.setColour(Blockly.Blocks.wifiserver.HUE);
  316. this.appendDummyInput()
  317. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  318. this.appendStatementInput('STACK');
  319. this.setTooltip('');
  320. this.contextMenu = false;
  321. }
  322. };
  323. Blockly.Blocks['ServerSend_create_with_item'] = {
  324. /**
  325. * Mutator bolck for adding items.
  326. * @this Blockly.Block
  327. */
  328. init: function() {
  329. this.setColour(Blockly.Blocks.wifiserver.HUE);
  330. this.appendDummyInput()
  331. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  332. this.setPreviousStatement(true);
  333. this.setNextStatement(true);
  334. this.setTooltip('');
  335. this.contextMenu = false;
  336. }
  337. };
  338. /**WiFi Server
  339. * receive JSON data from Client,and parse it into List
  340. * @param {variable} var the list name of JSON data parse into
  341. * @param {}
  342. */
  343. Blockly.Blocks['wifiServer_Receive'] = {
  344. init: function() {
  345. this.appendDummyInput()
  346. .appendField(Blockly.Msg.WIFISERVER_RECEIVE)
  347. this.appendDummyInput()
  348. .appendField(Blockly.Msg.WIFISERVER_RECEIVE_TO)
  349. .appendField(new Blockly.FieldTextInput("Data_receive"), "VAR")
  350. this.appendDummyInput()
  351. .appendField(Blockly.Msg.WIFISERVER_RECEIVE_LENGTH)
  352. .appendField(new Blockly.FieldDropdown([
  353. ["1", "1"],
  354. ["2", "2"],
  355. ["3", "3"],
  356. ["4", "4"],
  357. ["5", "5"],
  358. ["6", "6"],
  359. ["7", "7"],
  360. ["8", "8"],
  361. ["9", "9"],
  362. ["10", "10"],
  363. ["11", "11"],
  364. ]), "LENGTH")
  365. this.setColour(Blockly.Blocks.wifiserver.HUE);
  366. this.setPreviousStatement(true);
  367. this.setNextStatement(true);
  368. // this.setInputsInline(true);
  369. this.setTooltip("");
  370. this.setHelpUrl("");
  371. },
  372. };
  373. /**WiFi Server
  374. * receive JSON data from Client,and parse it into List
  375. * @param {variable} var the list name of JSON data parse into
  376. * @param {}
  377. */
  378. Blockly.Blocks['wifiServer_Receive_getValue'] = {
  379. init: function() {
  380. this.appendDummyInput()
  381. .appendField(" " + Blockly.Msg.WIFISERVER_RECEIVE_GET)
  382. .appendField(new Blockly.FieldTextInput("Data_receive"), "VAR")
  383. .appendField(" " + Blockly.Msg.WIFISERVER_RECEIVE_GET_LINE2)
  384. .appendField(new Blockly.FieldDropdown([
  385. ["0", "0"],
  386. ["1", "1"],
  387. ["2", "2"],
  388. ["3", "3"],
  389. ["4", "4"],
  390. ["5", "5"],
  391. ["6", "6"],
  392. ["7", "7"],
  393. ["8", "8"],
  394. ["9", "9"],
  395. ["10", "10"],
  396. ["11", "11"],
  397. ]), "INDEX")
  398. .appendField(" " + Blockly.Msg.WIFISERVER_RECEIVE_GET_LINE3)
  399. .appendField(new Blockly.FieldDropdown([
  400. [Blockly.Msg.WIFISERVER_RECEIVE_GET_INT, "Int"],
  401. [Blockly.Msg.WIFISERVER_RECEIVE_GET_STRING, "String"]
  402. ]), "TYPE")
  403. this.setColour(Blockly.Blocks.wifiserver.HUE);
  404. this.setOutput(true);
  405. this.setTooltip("");
  406. this.setHelpUrl("");
  407. },
  408. getBlockType: function() {
  409. var type = this.getFieldValue("TYPE");
  410. switch (type) {
  411. case "Int":
  412. return Blockly.Types.NUMBER;
  413. break;
  414. case "String":
  415. return Blockly.Types.TEXT;
  416. break;
  417. }
  418. return Blockly.Types.UNDEF;
  419. }
  420. }
  421. /*wifi server
  422. *if there is no url, set server with response when HTTP requests
  423. */
  424. Blockly.Blocks['wifiServer_onNotFound'] = {
  425. init: function() {
  426. this.appendValueInput("VAR")
  427. .appendField(Blockly.Msg.WIFISERVER_SETROUTER_ONNOTFOUND);
  428. this.setColour(Blockly.Blocks.wifiserver.HUE);
  429. // this.setOutput(true,Blockly.Types.TEXT.output);
  430. this.setPreviousStatement(true, null);
  431. this.setNextStatement(true, null);
  432. // this.setInputsInline(true);
  433. this.setTooltip("");
  434. this.setHelpUrl("");
  435. },
  436. };
  437. /*wifi server
  438. *server send to client
  439. */
  440. Blockly.Blocks['wifiServer_serverSend'] = {
  441. init: function() {
  442. this.appendDummyInput()
  443. .appendField(Blockly.Msg.WIFISERVER_SERVERSEND);
  444. this.appendValueInput("CODE")
  445. .appendField(Blockly.Msg.WIFISERVER_SERVERSEND_CODE);
  446. this.appendValueInput("TYPE")
  447. .appendField(Blockly.Msg.WIFISERVER_SERVERSEND_TYPE);
  448. this.appendValueInput("CONTENT")
  449. .appendField(Blockly.Msg.WIFISERVER_SERVERSEND_CONTENT);
  450. this.setColour(Blockly.Blocks.wifiserver.HUE);
  451. // this.setOutput(true,Blockly.Types.TEXT.output);
  452. this.setPreviousStatement(true, null);
  453. this.setNextStatement(true, null);
  454. // this.setInputsInline(true);
  455. this.setTooltip("");
  456. this.setHelpUrl("");
  457. },
  458. };
  459. /*wifi server
  460. *function handleClient() to wait for client connection
  461. *and handle clients' HTTP request
  462. */
  463. Blockly.Blocks['wifiServer_handleClient'] = {
  464. init: function() {
  465. this.appendDummyInput()
  466. .appendField(Blockly.Msg.WIFISERVER_HANDLECLIENT)
  467. this.setColour(Blockly.Blocks.wifiserver.HUE);
  468. // this.setOutput(true,Blockly.Types.TEXT.output);
  469. this.setPreviousStatement(true, null);
  470. this.setNextStatement(true, null);
  471. // this.setInputsInline(true);
  472. this.setTooltip("");
  473. this.setHelpUrl("");
  474. },
  475. };
  476. /*wifi server
  477. *wait for client connection
  478. */
  479. Blockly.Blocks['wifiServer_waitConnection'] = {
  480. init: function() {
  481. this.appendDummyInput()
  482. .appendField(Blockly.Msg.WIFISERVER_WAIT_CONNECTION)
  483. this.setColour(Blockly.Blocks.wifiserver.HUE);
  484. // this.setOutput(true,Blockly.Types.TEXT.output);
  485. this.setPreviousStatement(true, null);
  486. this.setNextStatement(true, null);
  487. // this.setInputsInline(true);
  488. this.setTooltip("");
  489. this.setHelpUrl("");
  490. },
  491. };